REMEMBER JAVA

MINTE9
2REMEMBER




Last update:   23-07-2021

Instance variables

p72 Instace variables: things the object knows (title, artist) Methods: things the object does (setTitle(), setArtist(), play()).
 RunCode
class LearningApp {
    
    public static void main(String[] args) {
    
        Dog d = new Dog();
        d.size = 40;
        d.bark();
    }
}

class Dog {
    
    int size;
    String name;
    
    void bark() {
        System.out.println("Ham Ham"); // Ham Ham
    }
}
... 14 lines
˄˄˄

JVM

p74 ! You start by declare a reference variable - Doq myDog. This tells the JVM to allocate space for a reference variable. This tells the JVM to name variable - myDog. The reference variable is forever of type Dog!
 
Dog d = new Dog(); // links the object and the reference.
... 1 lines
˄˄˄

Reference

p77 In this example, we have two Book reference variables and two Book objects.
 RunCode
class LearningApp {
    
    public static void main(String[] args) {
        
        // Step 1
        Book b = new Book(1); // object 1
        Book c = new Book(2); // object 2
        System.out.printf("%s %s \n", b.x, c.x); // 1 2

        // Step 2
        Book d = c; // object 3
        System.out.printf("%s %s %s \n", b.x, c.x, d.x); // 1 2 2

        // Step 3
        c = b;  // overwrite c

            // c is now reference to object 1
            // c reference to object 2 is destroyed
            // (eligible for Garbage Collection)

        System.out.printf("%s %s %s \n", b.x, c.x, d.x); // 1 1 2
    }
}

class Book {
    
    int x = 0;
    
    public Book(int x) { // contructor
        
        this.x = x;
    }
}
... 23 lines
˄˄˄
Task

 CompleteTask
/** 
 * Create a new Dog instance ...
 * Make the dog size = 10
 * Output dog size on the screen
 */
class LearningApp {
    
    public static void main(String[] args) {

        ...
    }
}

class Dog {

    public int size;
    public String name;
}
solution code
Questions    
No. 3   Arrays
CARD
02

        A B C D E F
🔔
1/5