1

A is another class outside of test

case 1:

public class Test{



      A testObj;

      public static void main(String[] args){
         testObj=new A();
         testObj.methodInsideClassA();
      }
}

case 2:

 public class Test{


      public static void main(String[]  args){
         A testObj = new A();
         testObj.methodInsideClassA();
      }
}

so whats the difference between them? And what should I use?

Razib
  • 10,965
  • 11
  • 53
  • 80
devilakos
  • 19
  • 4
  • 1
    `Case 1` testObj needs to be declared `static` to compile (hence, is a [static variable](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html)) – copeg May 31 '15 at 16:12
  • 1
    Not sure if it is `Déjà vu` or whether I have come across this question way too many times on this site that I am too lazy to even mark it as a duplicate. – Chetan Kinger May 31 '15 at 16:13
  • @copeg I ve compiled a case 1 like program without declaring testObj static – devilakos May 31 '15 at 16:30
  • @devilakos If you were able to compile it, then it wasn't "case 1" like. You should mind, that `main` is a `static` method. I guess your program (or the method which you mean was "case 1 like") wasn't static. – Tom May 31 '15 at 16:31
  • possible duplicate of [what is the difference between local and instance variables in Java](http://stackoverflow.com/questions/2088299/what-is-the-difference-between-local-and-instance-variables-in-java) – Tom May 31 '15 at 16:35
  • @Tom Sorry,my bad i used non-static A from a non static method called from main(with the use of a Test object) – devilakos May 31 '15 at 16:38
  • @devilakos Yes, that would work. Each non-static method can access every instance field of the class. A static method needs a reference to an instance of the "own" class to do that. – Tom May 31 '15 at 16:40

3 Answers3

1

Case 1

testObj is a class-level variable.

Case 2

testObj is a local variable. A local variable is the one that is declared within a method or a constructor.

One important distinction between class-level variable and local variable is that access specifiers can be applied to class-level variables only and not to local variables.

Bacteria
  • 8,406
  • 10
  • 50
  • 67
0

If this object should be shared between some method runs, you should use the first options. In other cases (temporary object), you should use the second.

Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
0

The difference between of the two declaration in the scope of the instance/object of A.

In first case the testObj is accessible from the all method of class Test In second case the testObj is accessible only from the main() method of class Test. While main() method terminated testObj also removed from the memory.

What you have to use it depends on situation. Suppose in some case you require the instance of A (that is testObj ) in all over of your Test class - means several methods of class Test uses the same instance of A. Then you declare testObj outside of all methods of class Test - that is as an instance variable.

When different method of class Test requires different instance of A then you may use method local variable as in case 2.

Razib
  • 10,965
  • 11
  • 53
  • 80
  • Since when did instance variables stop relying on `this`? – Chetan Kinger May 31 '15 at 16:16
  • so this works like the normal variable(int,float,char etc) declaration thanks.. and another question...if i use the first case (create an object on class level) will i have to asign a value to it everytime i want to use it on another method of my class or its value will stay in memory after initialization? – devilakos May 31 '15 at 16:28