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

Thread: Why can't I get my variable to display?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why can't I get my variable to display?

    I trying to display the value of a variable from a method, but I don't know why it will I can't do it. Can anyone help please?...

    [Highlight]


    //Java extension package
    import javax.swing.JOptionPane;

    public class Palindrome
    {

    public static void main(String[] args)
    {
    int number = 0;
    int palindrome = 0;

    palindrome = retriveInput(number);

    display(palindrome); //Error: Cannot make a static reference to the non-static method display(int) from the type Palindrome

    }

    public static int retriveInput(int numInput)
    {
    String userInput = "";

    userInput = JOptionPane.showInputDialog("Enter five digit number:");
    numInput = Integer.parseInt(userInput);

    return numInput;
    }

    public void display(int test)
    {
    System.out.print(test);
    }

    }

    [Highlight]


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Why can't I get my variable to display?

    Hi geovanniluna,
    I suggest that you do a quick Google search for "Cannot make a static reference to the non-static method" and perhaps look more into what the static keyword means and how/when it should be used.

  3. #3
    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: Why can't I get my variable to display?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why can't I get my variable to display?

    If you are getting errors you should post the entire error message.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why can't I get my variable to display?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why can't I get my variable to display?

    the method "display" is non-static.. but this method is call inside a static main method.. because of this error occurs..
    change the method "display" to static..

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Why can't I get my variable to display?

    public static int retriveInput(int numInput)

    Just like Nijan said...

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  2. What is the difference between instant variable and instance variable?
    By avistein in forum Java Theory & Questions
    Replies: 7
    Last Post: January 6th, 2013, 11:42 PM
  3. Replies: 0
    Last Post: October 30th, 2012, 10:06 AM
  4. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  5. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM