Calling a method on an object from a class in comparison with a method

This may be a really basic question, but it puzzles me at this point in learning Java. I have the following code snippet:

package com.soti84; import java.util.ArrayList; public class InvokeMethod { public static void main(String[] args) { ArrayList<String> exams= new ArrayList<String>(); exams.add("Java"); exams.add("C#"); } } 

If I move the line that creates the ArrayList object and calls this object outside the method, the line that creates the object is fine, but the call to the add () method on the object is not allowed. Why is this?

  package com.soti84; import java.util.ArrayList; public class InvokeMethod { ArrayList<String> exams= new ArrayList<String>(); exams.add("Java"); exams.add("C#"); public static void main(String[] args) { } } 

Thanks.

+6
source share
4 answers

You simply cannot make this code outside of methods. If you want to do this, you will need an initialization block or a static block.

 public class InvokeMethod { ArrayList<String> exams= new ArrayList<String>(); { exams.add("Java"); exams.add("C#"); } 

Now this block will be executed when you create the instance. If your variable was static, you can make this block static (just add static before this block). A static block starts when your class is initialized. These blocks can be quite convenient if you need a filled static list / map. Of course, everything that is convenient in programming is probably bad practice, and the same thing here, these blocks are unhappy with some people, they can be quite dangerous and lead to hard-to-reach errors (mainly, about the execution order).

+3
source

In the two examples, you are trying to achieve two completely different things.

In the first example, you declare an ArrayList inside the main method, so the list area will be exactly that method. The closing class is completely unrelated to this ArrayList .

In the second, you try to create a data item named exams in the InvokeMethod class. This means that each instance of this class will have its own list.

Adding elements does not work, because "from methods" can only be declaration and initialization. To fix this, you can use the initialization block :

 public class InvokeMethod { ArrayList<String> exams = new ArrayList<String>(); { exams.add("Java"); exams.add("C#"); } public static void main(String[] args) { } } 

or, class constructor:

 public class InvokeMethod { ArrayList<String> exams = new ArrayList<String>(); public InvokeMethod() { exams.add("Java"); exams.add("C#"); } public static void main(String[] args) { } } 

Note. . You can also extract this list from the main method through an instance of the InvokeMethod class:

 public class InvokeMethod { ArrayList<String> exams = new ArrayList<String>(); public InvokeMethod() { exams.add("Java"); exams.add("C#"); } public static void main(String[] args) { InvokeMethod invokeMethod = new InvokeMethod(); System.out.println(invokeMethod.exams.toString()); invokeMethod.exams.add("Delphi"); System.out.println(invokeMethod.exams.toString()); } } 

will print

 [Java, C#] [Java, C#, Delphi] 
+1
source

This is because "you are calling it from outside a function / method"

 ArrayList<String> exams= new ArrayList<String>(); 

The line above means that you declare it as a property of the object. which means that you can only access it inside the method.

If you are going to post in the main

following:
 exams.add("Java"); exams.add("C#"); 

This should work fine, although you have declared exams outside the method.

0
source

According to the Java Language Specification , a body class declaration has an Instance Initializer, but does not have a method call. Therefore, in your example, ArrayList<String> exams= new ArrayList<String>(); allowed inside the class body, but not exams.add("Java");

Excerpt from JLS:

 ClassBody: { ClassBodyDeclarationsopt } ClassBodyDeclarations: ClassBodyDeclaration ClassBodyDeclarations ClassBodyDeclaration ClassBodyDeclaration: ClassMemberDeclaration InstanceInitializer StaticInitializer ConstructorDeclaration ClassMemberDeclaration: FieldDeclaration MethodDeclaration ClassDeclaration InterfaceDeclaration ; 
0
source

All Articles