Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: static context

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question static context

    Hello

    public class test
    {
    	public static void main(String[] args)
    	{
    		fail f = new fail();
    	}
     
    	class fail
    	{
     
    	}
    }

    I got this: non-static variable this cannot be referenced from a static context

    public class test
    {
    	public static void main(String[] args)
    	{
    		test yeah = new test();
    	}
    }

    It works


    Both variables aren't static. Why in the first case it doesn't work?

    Thank you very much


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: static context

    You must declare a nested class static if you try to instantiate it from a static context (eg main). Inner classes (non-static nested classes) have access to the variables in the enclosing class...the way in which you are trying to instantiate the inner class there is no enclosing object of the outer class.
    Suggested reading: Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. The Following User Says Thank You to copeg For This Useful Post:

    mydoom (July 25th, 2011)

Similar Threads

  1. Replies: 6
    Last Post: July 18th, 2011, 08:48 AM
  2. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  3. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  5. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 01:56 PM

Tags for this Thread