Chaps, sorry I have a silly question. I am not quite sure if this is right, but what is the differenct between the two below:

public class myclass{

    public static void main( String args[] ){
    boolean variable1 = false;
    boolean variable2 = false;
    ...
    System.out.print(variable1 + " and " + variable2)
    }

and

public class myclass{
    boolean variable1 = false;
    boolean variable2 = false;
    public static void main( String args[] ){   
    ...
    System.out.print(variable1 + " and " + variable2)
    }

Does it mean that in the second snippet the variables are static? Also will I be able to print both of the variables in both snippets without compilation errors?
thanks

In the first set of declarations, the variables are local to main(); they only exist inside main(), and only for the time that the program is running. You can print the variables from main() just as you have them now.

In the second set of declarations, the variables are instance variables; they would exist within the scope of an object declared as that type, and each object would have their own versions of the variables. They would not be visible in main() as written, as main() is a static function and can only see variables that are local to it, or have been declared static as part of the class. To access the instance variables, you would need to instantiate a variable of the class, and access them from that object:

public class myclass{
    boolean variable1 = false;
    boolean variable2 = false;

    public static void main( String args[] ){
        myclass myobject = new myclass();
        System.out.print(myobject.variable1 + " and " + myobject.variable2);
    }
}

(Note the way I indented the lines inside of main(), BTW; as a rule, you want to indent every level of nesting, and dedent when the nested scope ends.)

To declare the variables as class variables, you would need to use the keyword static in their declarations, just as you do with class methods.

    public class myclass{
        static boolean variable1 = false;
        static boolean variable2 = false;

        public static void main( String args[] ){
            System.out.print(myclass.variable1 + " and " + myclass.variable2);
        }
    }

I hope this clarifies things at least a little.

brilliant, yep crystal clear! no wonder i was getting all sort of errors
thanks

This question has already been answered. Start a new discussion instead.