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

Thread: Simple question- nonstatic variable?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Simple question- nonstatic variable?

    import java.util.Scanner;
     
    public class Prog
    {
        public static void main (String[] args)
        {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter num");
            int num = scan.nextInt();
     
            Tutorial2 info = new Tutorial2();
            info.message(num);
        }
     
     
        public class Tutorial2
        {
            public void message(int number)
            {
     
                System.out.printf("The number you entered was %d", number);
     
     
            }
     
     
     
        }
     
    }



    I get a compile time error- nonstatic variable- this cannot be referenced from a static context.


    Thanks in advance.


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Simple question- nonstatic variable?

    It goes away if you make the class Tutorial2 static. Why the entire class has to be static, I'm not totally sure myself.

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

    Default Re: Simple question- nonstatic variable?

    Copy and paste the full and exact message.

    --- Update ---

    Quote Originally Posted by GoodbyeWorld View Post
    Make message(int number) be a static method and the issue will go away.
    Bad advice!

    They have created a Tutorial2 object and have called the method non-statically which is the correct way to go.
    Improving the world one idiot at a time!

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

    aussiemcgr (October 21st, 2013)

  5. #4
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Simple question- nonstatic variable?

    Prog.jpg
    Quote Originally Posted by Junky View Post
    Copy and paste the full and exact message.

    --- Update ---


    Bad advice!

    They have created a Tutorial2 object and have called the method non-statically which is the correct way to go.

    What I posted is the exact error. I got this program from the tutorial at Intro to Java Programming 19 - Multiple Classes - YouTube I may have copied something wrong. I am using BlueJ.

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

    Default Re: Simple question- nonstatic variable?

    No it is not the EXACT message. java provides very informative error messages that usually extend over several lines, of which provides the line of code where the error occur.

    If you are just hovering your mouse over the code in an IDE then try compiling the code and get the full message.
    Improving the world one idiot at a time!

  7. #6
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Simple question- nonstatic variable?

    Quote Originally Posted by Junky View Post
    Copy and paste the full and exact message.

    --- Update ---


    Bad advice!

    They have created a Tutorial2 object and have called the method non-statically which is the correct way to go.
    Yes, at first I didn't see that it was part of a separate class and thought it was just another method of the same class.

    It compiles if you do this:

    import java.util.Scanner;
     
    public class Prog
    {
       public static void main (String[] args)
       {
          Scanner scan = new Scanner(System.in);
          System.out.println("Enter num");
          int num = scan.nextInt();
     
          Tutorial2 info = new Tutorial2();
          info.message(num);
       }
     
     
       public static class Tutorial2
       {
          public  void message(int number)
          {
     
             System.out.printf("The number you entered was %d", number);
     
     
          }
     
     
     
       }
     
    }

    From what I just read on the exact reason for the error, apparently you can call an "upper class" method in the "lower class" but if you try and call a "lower class" method in the "upper class", then the "lower class" must be static.

    Static nested class in Java, why? - Stack Overflow

  8. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    TSSF44 (October 20th, 2013)

  9. #7
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Simple question- nonstatic variable?

    Quote Originally Posted by Junky View Post
    No it is not the EXACT message. java provides very informative error messages that usually extend over several lines, of which provides the line of code where the error occur.

    If you are just hovering your mouse over the code in an IDE then try compiling the code and get the full message.
    Here is the full message. Sorry about that. I just dont understand how the user in that tutorial did the exact same thing and it compiled. Unless I made an error copying?
    Attached Images Attached Images

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

    Default Re: Simple question- nonstatic variable?

    The best solution would be to NOT have Tutorial2 as an inner class. Move it out to its own file.

    BTW that is still not the full error message. It is some sanitised version the IDE throws at you. As I said above you should compile your code to get the full message.
    Improving the world one idiot at a time!

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

    TSSF44 (October 20th, 2013)

  12. #9
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Simple question- nonstatic variable?

    Quote Originally Posted by Junky View Post
    The best solution would be to NOT have Tutorial2 as an inner class. Move it out to its own file.

    BTW that is still not the full error message. It is some sanitised version the IDE throws at you. As I said above you should compile your code to get the full message.
    Thank you, but maybe I should just stop using Bluej. Ive been compiling the whole time and those are the only errors that Bluej gives me.



    Thanks for the help to both of you. I won't just inner classes anymore.

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

    Default Re: Simple question- nonstatic variable?

    Really?

    Other IDE's (Eclipse and Netbeans) usually display the full error messages in a separate pane called Console.
    Improving the world one idiot at a time!

  14. #11
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple question- nonstatic variable?

    I agree the sixth floor.

  15. #12
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Simple question- nonstatic variable?

    I disagree with the tutorial you were looking at (if this is indeed what they told you to do). The Prog class contains the main method, but you have to remember that the Prog class itself is an object class, so it can contain a constructor and Prog objects can be created. Knowing this allows you avoid all the static garbage from the main method (the static keyword has its purposes, but not for what you are attempting to do).
    You can make Tutorial2 non-static if you reference it from inside a non-static method. But, how can you use that method in the main without making the method static? Easy: use the Prog constructor instead and just create a new Prog() object in your main:
    import java.util.Scanner;
     
    public class Prog {
     
        public Prog() {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter num");
            int num = scan.nextInt();
     
            Tutorial2 info = new Tutorial2();
            info.message(num);
        }
     
        public static void main(String[] args) {
            new Prog();
        }
     
        public class Tutorial2 {
            public void message(int number) {
                System.out.printf("The number you entered was %d", number);
            }
        }
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  16. The Following User Says Thank You to aussiemcgr For This Useful Post:

    TSSF44 (October 24th, 2013)

  17. #13
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Simple question- nonstatic variable?

    Thanks everyone, I learned a lot in this thread.

Similar Threads

  1. Variable not initialized error in IF statements. probable simple fix.
    By GStateSwish in forum Loops & Control Statements
    Replies: 1
    Last Post: July 4th, 2013, 06:25 PM
  2. Variable question from a beginner
    By JimmyJ1776 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2013, 12:55 PM
  3. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  4. simple question...
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2010, 07:29 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM