#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    33
    Rep Power
    58

    Non-static variable static context


    im just assigning the variable in main, how is that static context?
    Code:
    import java.lang.Float;
    import java.lang.String;
    public class DCR
    {
    	float resistivity;
    	float strandingIncrement;
    	float circularMills;
    	float resistance;
    	public static void main (String[] args) throws Exception
    	{
    		resistivity = Float.parseFloat(args[0]);
    		strandingIncrement = Float.parseFloat(args[1]);
    		circularMills = Float.parseFloat(args[2]);
    		new DCR();  
    		System.out.println(resistance);  
    	}
    	
    	public float DCR() throws Exception
    	{
    		resistance =resistivity*strandingIncrement*1000/circularMills;
    	}
    }
    Code:
    DCR.java:31: non-static variable resistivity cannot be referenced from a static context         resistivity = Float.parseFloat(args[0]);         ^ DCR.java:32: non-static variable strandingIncrement cannot be referenced from a static context         strandingIncrement = Float.parseFloat(args[1]);         ^ DCR.java:33: non-static variable circularMills cannot be referenced from a stati c context         circularMills = Float.parseFloat(args[2]);         ^ DCR.java:35: non-static variable resistance cannot be referenced from a static c ontext                 System.out.println(resistance);
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    84
    Rep Power
    125
    It is a static context because main() is static method. Java is an object oriented language, which means that everything needs to be accessed in the context of an object if it is not static. So you would need to create a DCR object first and then work with that. The following works, with the DCR() method renamed for clarity. Hope this helps.

    java Code:
     
    public class DCR
    {
    	float resistivity;
    	float strandingIncrement;
    	float circularMills;
    	float resistance;
    	public static void main (String[] args) throws Exception
    	{
            DCR dcr = new DCR();
            dcr.resistivity = Float.parseFloat(args[0]);
    		dcr.strandingIncrement = Float.parseFloat(args[1]);
    		dcr.circularMills = Float.parseFloat(args[2]);
            dcr.calculate();
    		System.out.println(dcr.resistance);
    	}
     
    	public void calculate() throws Exception
    	{
    		resistance =resistivity*strandingIncrement*1000/circularMills;
    	}
    }
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSn
    Posts
    6,878
    Rep Power
    3890
    The "static context" comes from the fact that "main" is a static method. Your variables are not static.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    33
    Rep Power
    58
    thank you hglasgow, not only did you give me code but now i understand why
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    33
    Rep Power
    58
    and you as well ishnid
  10. #6
  11. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    16
    all programming languages support the static thing...
    static variables are accessible to all objects of that class they are shared as only only copy is shared between the class objects . And what the static methods ... you can access them without using objects eg: <class-name>.<static-method-name>()
    So what you have to remember that you can only access static variables inside static methods....
    And in your problem main() is static. So you now know why..

    Comments on this post

    • jzd disagrees : Sorry, but static variables can be accessed in any class method, not just static methods.
    no one can become perfect by merely ceasing the act
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    33
    Rep Power
    58
    ah yes, that makes it very clear, tyvm
  14. #8
  15. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    16
    jzd disagrees: Sorry, but static variables can be accessed in any class method, not just static methods.
    Whats wrong.... thats what i have said

    Originally Posted by codeJ
    you can only access static variables inside static methods....
    no one can become perfect by merely ceasing the act
  16. #9
  17. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Sep 2007
    Location
    outside Washington DC
    Posts
    2,576
    Rep Power
    3703
    Originally Posted by codeJ
    static variables are accessible to all objects of that class they are shared as only only copy is shared between the class objects
    actually, you can not rely upon this. In simple cases, there is typically only one instance of the class, so there is only one instance of the static variables. But there can be multiple class loaders in use, and each may load the class independently. You see this with JBoss and other JEE containers. Its actually very hard to make really, really sure that there is only one instance of the class. This is one major reason why the Singleton pattern is discouraged in modern code.

    Comments on this post

    • Yawmark agrees
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,939
    Rep Power
    3120
    Originally Posted by codeJ
    Whats wrong.... thats what i have said
    Yes, you said "you can only access static variables inside static methods....".

    This is a false statement. The true statement is that you can access static variables in any method, not just in static methods.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0
    Originally Posted by jzd
    Yes, you said "you can only access static variables inside static methods....".

    This is a false statement. The true statement is that you can access static variables in any method, not just in static methods.
    I can see your confusion, you can interpret his sentence in two ways. If he had written it like this instead it would be less confusing: "Inside static methods you can only access static variables"

    As it is now you can interpret it that way (which is correct) but you can also see it as "The only place where you can access the static variables is inside the static methods" (which is a false statement)

    Comments on this post

    • ishnid agrees : Good point. I read it the same way as jzd did.
  22. #12
  23. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5057
    I can see your confusion, you can interpret his sentence in two ways. If he had written it like this instead it would be less confusing: "Inside static methods you can only access static variables"
    Except that isn't quite true, either. To be overly pedantic, neither instance variables nor local variables are considered static and both can be accessed within a static method. In the case of an instance variable, one must simply provide a reference to an instance that makes that variable visible. In the case of a local variable, one can access it freely within a static method without qualification, as long as it is in scope.

    A more appropriate way to phrase the point would be along the lines of the compiler error; namely, that one cannot access non-static members from a static context.

    Just my opinion, for whatever it's worth.

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

IMN logo majestic logo threadwatch logo seochat tools logo