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);
Instructor
Ashish Thakur 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.
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?
Instructor
Ashish Thakur 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