Hey Everyone,

I want to instantiate an ArrayList in java. I realize the method to doing this is:

ArrayList<String> determiners = new ArrayList <String>();
determiners.add("A");

Where "A" is added to the arraylist once created. However, the problem I'm having is that the .add method doesn't work unless in a main method. What is the santax for making it work in a class without the main method.

Thanks

Recommended Answers

All 8 Replies

add() can always work without main method.. this is a simple code showing that happening.

import java.util.ArrayList;


public class tester {

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

    public tester(){
        arr.add("sure ");
        arr.add("it ");
        arr.add("does");
        arr.add("... see :) ");
        for(String s : arr)
            System.out.print(s);
    }

    public static void main(String[] args){
        tester t = new tester();
    }

}

hai milkman93

as somjit{} said along with that, you may use static block concept for your requirement like as follows

class SampleTest
{
  static{
       ArrayList<String> arr = new ArrayList<String>()
       arr.add("A"); 
       arr.add("B"); 
       // code for printing these array list values values
  }
}

and these static blocks are executed while class is loading without creating object

let me know if you have any dioubts in my clarification

but these are only for static variables, hence the term 'static' block. for instance variables, why not using the constructor of the class?

in the end, you'll need a main method to start your application, doesn't mean all your logic needs to be in there.

Static initialisers are OK, but in radhakrishna.p's example watch out for the scope of arr!
You can also use an instance initialiser http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html which (to answer stultiske's question) save having to duplicate code between multiple constructors, and can be used in anonymous inner classes.

Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.

umm.. whats the difference between class variables and instance variables? i thought they were the same ? i read that we work with instances of the class , instead of the class itself.. so i suppose then the exceptions to this would mean a static class ? like math?

somjit: until you actually instantiate your object's instance, non-static members 'll not be known, simply because the JVM 'll not know the instance to which to attach them.

class => static, they depend on the class, not on an instance there-of, equal for every instance of the class
instance => they depend on an instance, each instance can have it's own unique values

an instance variable can not be initialized within a static block, simply because it doesn't exist yet.
as JamesCherrill also stated: the arr variable doesn't even exist outside of the static block, so, even though it IS a correct answer to the question, it's also a bit a useless one.

umm.. whats the difference between class variables and instance variables? i thought they were the same

There's no such thing as a "class variable" per se. He means a static data member, which is definitely different from an instance variable in that it exists independently of any instance of the class.

Thanks somjit{} ! That solved the problem and clarified things for future cases.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.