I have doubt in the below code from the excercise module 3.How can we use i,j global non static variable in the static function add(int i,int j)? | Selenium Forum
D
dasmadhusmita99 Posted on 12/03/2020

public class quiz1 {
int i;
int j;
public static void main(String[] args) {
quiz1 t1 = new quiz1();
t1.i=200;
t1.j=100;
add(t1.i,t1.j);
System.out.print(t1.i);
System.out.print(t1.j);
}

public static void add(int i,int j) {
i=i+100;
j=j+100;
System.out.print(i);
System.out.print(j);


A
Ashish Replied on 13/03/2020

Whenever we declare a variable inside the function, even if it is declared globaly. It automatically ignores the global declaration and the values are retained for the local variable only.


Y
ysy001 Replied on 14/10/2020

Hi Ashish,

How can we instantiate an object of a class within that same class itself? In the above example, we are defining class quiz1, and within that class, inside the main() function, we are instantiating object 't1' of class quiz1. Isn't the main function part of the class quiz1?


A
Ashish Replied on 16/10/2020

Yes But we can do it.

Java allows it

With that object you can access the non static functions from the main class