static vs non static java code example
Example 1: can you run non static method on main
public Class Obj{
public void nonStaticMethod(){
}
public static void main(string args[]){
Obj obj = new Obj();
obj.nonStaticMethod
}
}
Example 2: static vs non static java
public class MyClass {
private static int myInt;
static {
myInt = 1;
}
public static int getInt() {
return myInt;
}
}
System.out.println(MyClass.getInt());
public class MyClass {
private int myInt;
public MyClass() {
myInt = 1;
}
public int getInt() {
return this.myInt;
}
}
MyClass myObj = new MyClass();
System.out.println(myObj.getInt());