Saturday, December 3, 2011

Local/Instance/Class Variables

There are three kinds of Java variables:


  • Local variables

declared in a method, constructor, or block. Local variables are not visible outside the method.


  • Instance variables,member or field variables

declared in a class, but outside a method.They are also called member or field variables.
an instance variable is created when an object is created and destroyed when the object is destroyed
Visible in all methods and constructors of the defining class


  • Class/static variables

declared with the static keyword in a class, but outside a method
There is only one copy per class, regardless of how many objects are created from it
http://leepoint.net/notes-java/data/variables/45local-inst-class.html



Java Static Variables and Methods
http://www.youtube.com/watch?v=gTk_F61_-9k&feature=related







10. Accessing non-static member variables from static methods (such as main)

Many programmers, particularly when first introduced to Java, have problems with accessing member variables from their main method. The method signature for main is marked static - meaning that we don't need to create an instance of the class to invoke the main method. For example, a Java Virtual Machine (JVM) could call the class MyApplication like this :-

MyApplication.main ( command_line_args );

This means, however, that there isn't an instance of MyApplication - it doesn't have any member variables to access! Take for example the following application, which will generate a compiler error message.

public class StaticDemo
{
        public String my_member_variable = "somedata";
        public static void main (String args[])
        {
// Access a non-static member from static method
                System.out.println ("This generates a compiler error" +
my_member_variable );
        }
}
If you want to access its member variables from a non-static method (like main), you must create an instance of the object. Here's a simple example of how to correctly write code to access non-static member variables, by first creating an instance of the object.

public class NonStaticDemo
{
        public String my_member_variable = "somedata";

        public static void main (String args[])
        {
                NonStaticDemo demo = new NonStaticDemo();

// Access member variable of demo
                System.out.println ("This WON'T generate an error" +
                        demo.my_member_variable );
        }
}



http://www.javacoffeebreak.com/articles/toptenerrors.html









FAQ002: non-static method cannot be accessed from a static context
http://www.youtube.com/watch?v=bWxn_h4T5xI
when you try to access non-static variable through a static method you have to have object reference means that you have to create that object firts to access non-static variable.




  • Why is access to non-static variables not allowed from static methods in Java

You can not access non-static data from static context in Java simply because non-static variables are associated with a particular instance of object while Static is not associated with any instance

http://javarevisited.blogspot.com/2012/06/20-design-pattern-and-software-design.html#ixzz2B5eppfnB





No comments:

Post a Comment