method without static in java code example
Example: static vs non static java
// Static vs Object Called
// Static Methods don't require you to call a constructor
// Initialize statically
public class MyClass {
private static int myInt;
static {
myInt = 1;
}
public static int getInt() {
return myInt;
}
}
// Then call it
System.out.println(MyClass.getInt());
// VS Constructor
public class MyClass {
private int myInt;
public MyClass() {
myInt = 1;
}
public int getInt() {
return this.myInt;
}
}
// Then call it
MyClass myObj = new MyClass();
System.out.println(myObj.getInt());