Unit 2: Objects presentation
Tri 1 Objects lesson supplemental
public class Main {
int x;
public Main(int y) { // this is the constructor, which sets the value of y
x = y;
}
public static void main(String[] args) { //
Main myObj = new Main(5);
System.out.println(myObj.x);
}
}
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj1 = new Main(); // object 1
Main myObj2 = new Main(); // object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
}
public class Main {
static void myMethod() {
System.out.println("I was just executed!");
}
public static void main(String[] args) {
myMethod();
}
}