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 6 of 6

Thread: non static variable this cant be referenced from a static context

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default non static variable this cant be referenced from a static context

    public class Class1 {
     
        private final class Class3 {
     
            public Class3() {
     
                method();
            }
     
            public void method() {
     
                System.out.println("123");
            }
        }
     
        public static void main(String[] args) {
     
            Class3 class3 = new Class3();
        }
    }

    i dont get it why does it gave me a static error
    Last edited by chronoz13; June 20th, 2011 at 02:00 PM.


  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: non static variable this cant be referenced from a static context

    You are attempting to reference a non-static inner class from a static context. Either access class 3 from the outer class, or declare class 3 as static.

  3. #3
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: non static variable this cant be referenced from a static context

    the class that you created in your main method is not static, its a "private final class"

    public class Class1 {
     
        private final class Class3 {
     
            public Class3() {
     
                method();
            }

    try creating Class1 instead of Class3.

    By the way what is the logic here having a inner class?

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: non static variable this cant be referenced from a static context

    it has something to do with a program that im creating, i encountered a problem i need to solve, so im trying to implement it in a shorter code.. so i was looking for a clarification for this kind of error

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: non static variable this cant be referenced from a static context

    For a quick and dirty test, you can make Class3 static.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: non static variable this cant be referenced from a static context

    Your problem is thinking "The main method and Class3 are both inside Class1 so how come the main method cannot see it?"

    consider the following:
    class Foo {
        public static void main(String[] args) {
            Class3 class3 = new Class3();
        }
    }
    All I did was place your main method in a completely different class. How is it supposed to find Class3? It has no idea where it is. As a general rule each class should be in its own file unless you are 1000% sure it needs to be an inner class. The reason you make a class an inner class is because it only needs to be used by its enclosing outer class. As soon as you try to use it outside that class (as you have done in the main method) you break that rule.

  7. The Following User Says Thank You to Junky For This Useful Post:

    chronoz13 (June 21st, 2011)

Similar Threads

  1. non-static variable
    By frozen java in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 15th, 2011, 06:18 PM
  2. 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
  3. non-static variable, addActionListener()
    By bobbyrne01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 10:58 AM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  5. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 04:36 AM