Exercise 3 question | Selenium Forum
M
Posted on 08/10/2015
As per below program in your exercise , I have following doubt.


public class Test {

Test t1= new Test();
int i;
static int j;
static Test t2 = new Test();
public static void main(String[] args) {
t1.i=10; //1
i=19; //2
j=10; //3
t2.i=19; //4

}
}


can we create object outside main method. If i am creating object inside public static void main function , are they static or non static by nature?

In above program if I am using the word Static where object is created, I am getting no compilation error.

please explain the reason for same ,

M
Replied on 08/10/2015

[quote:r5dqxasl]In above program if I am using the word Static where object is created, I am getting no compilation error.[/quote:r5dqxasl]
any static method in java can only access static variables. hence i is giving an error when non static variable are tried to be accessed.


[quote:r5dqxasl]object inside public static void main function , are they static or non static by nature?[/quote:r5dqxasl]

they are static inside the main method.