Pages

Non-Static Members

---------------------------------------------------------------------------
Non static
---------------------------------------------------------------------------
Note :
non-static member will not load in memory while loading class,whenever object get created then every non-static member loads in memory.
1)
class A
{
 int i;
 public static void main(String[] args) 
 {
  A a1=new A();
  System.out.println(a1.i);
 }
 
}
OP:
0
---------------------------------------------------------------------------
2)
class B
{
 void test()
 {
  System.out.println("From test");
 }
 public static void main(String[] args) 
 {
  B b1=new B();
  b1.test();
  System.out.println("Hello world");
 }
 
}
---------------------------------------------------------------------------
3)
class C
{
 int i;
 static void test()
 {
  C c1=new C();
  System.out.println(c1.i);
 }
}
//compilation successfull
runtime exception due to main method missing
---------------------------------------------------------------------------
4)
class D
{
 void test1()
 {
         System.out.println("hello");
 }
 static void test2()
 {
  D d1=new D();
  d1.test1();
 }
}
//test2() only will load in the memory,while executation only main method runs by default other method only load but not execute while loading ,
//while loading calling test1() will not invoke from test2,because test2 is not executing.
Exception
---------------------------------------------------------------------------
5)
class E
{
 int i;
 static //static block loads in memory and also get execute before main method,top to bottom
 {
  E e1=new E();
  System.out.println(e1.i);
 }
}
//0
//main method exception
---------------------------------------------------------------------------
6)
class F
{
 int i;
 void test1()
 {
 }
 public static void main(String[] args)
 {
  F f1=new F();
  f1.i=10;
  f1.test1();
  
 }
}
---------------------------------------------------------------------------
7)
class G
{
 void test1()
 {
            System.out.println("TEST1");
 }
 static 
 {
  G obj=new G();
  obj.test1();
 }
}
/*
TEST1
Exception in thread "main" java.lang.NoSuchMethodError: main*/
---------------------------------------------------------------------------
8)
class H
{
 int i;
 public static void main(String[] args)
 {
  H obj=new H();
  System.out.println(obj.i);
  obj.i=10;
  System.out.println(obj.i);
 }
}
OP:
0
10
---------------------------------------------------------------------------
9)
class I
{
 int x;
 int y=10;
 public static void main(String[] args)
 {
  I obj=new I();
  System.out.println(obj.x);
  System.out.println(obj.y);
  obj.x=20;
  obj.y=40;
  System.out.println(obj.x);
  System.out.println(obj.y);
 }
}
OP:
0
10
20
40
---------------------------------------------------------------------------
10)
class K
{
 int i;
 public static void main(String[] args)
 {
  K k1=new K();
  k1.i=10;
  K k2=new K();
  k2.i=20;
  System.out.println(k1.i);
  System.out.println(k2.i);
 }
}
OP:
10
20
---------------------------------------------------------------------------
11)
class A
{
 int i;
 public static void main(String[] args)
 {
  A a1=new A();
  System.out.println(a1.i);
  A a2=new A();
  System.out.println(a2.i);
  A a3=new A();
  System.out.println(a3.i);
  a1.i=10;
  a2.i=20;
  a3.i=30;
  System.out.println(a1.i);
  System.out.println(a2.i);
  System.out.println(a3.i);
 }
}
---------------------------------------------------------------------------
12)
class B
{
 int i;
 double d;
 boolean b;
 public static void main(String[] args)
 {
  B b1=new B();
  System.out.println(b1.i);
  System.out.println(b1.d);
  System.out.println(b1.b);
 }
}
/*
0
0.0
false
*/


No comments:

Post a Comment