6 Replies - 517 Views - Last Post: 09 October 2012 - 12:30 AM Rate Topic: -----

#1 holidaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-September 12

Error: non-static method cannot be referenced from static context

Posted 08 October 2012 - 10:34 PM

The code below is supposed to use a command-line arguments(String and Int) and then display panes with the information. The first time the pane is displayed it shows the original toString print. The second time the pane is supposed to display the modified toString print. I am to follow the following rule for both the phd and birthday method.

Add a (non-static) method to the Person class that adds..."insert string or integer change"...to Person object. The name of this method should be birthday()/phd(). The return type is void. The method has no parameters.

I am getting the following error after I have finished coding and not sure what to adjust as I have moved stuff around to no avail. Any advice? Below is the Error recieved for lines 16 and 17.

PersonCommand.java:24: error: non-static method phd() cannot be referenced from a static context
PersonCommand.java:24: error: non-static method birthday() cannot be referenced from a static context




import javax.swing.JOptionPane;

   public class PersonCommand {
      public static void main(String[ ] args) {
		
	int age1 = Integer.parseInt(args[1]);
			
       	//creates one person objects
         Person person1 = new Person(args[0], age1);
      	
			//Prints the first JPane with relative data fields
			String display1 = person1.toString();
			JOptionPane.showMessageDialog(null, display1);
			
			//Calls phd and birthday method
			Person.birthday();//Calls birthday method
			Person.phd();//Calls phd method
			JOptionPane.showMessageDialog(null, display1);

   	}//end of class
	}
/*Stores the name and age of the person*/
   class Person{
    //data fields that store each object's data
      private String name;
      private int age;
   	
   /** Constructor - Used To Create EAch Object & Initialize DAta Fields.
   * @param n1 is the Persons name 
   * @param a1 is the Persons age*/  
      public Person(String n1, int a1){
         name = n1;
         age = a1; 
      }
   	
   //Used to Display The Data Stored In EAch Object's DAta Field.
	public String toString(){
		String personString = name + " is " + age + " years old.";
		return personString;
	}
	//Method used to display age 	
	public void phd(){
		String n1 = "Dr. " + name;
		return;
	}
	//Method used to display age 	
	public void birthday(){
		Integer a1 = age + 1;
		return;
   }
  }
 


Is This A Good Question/Topic? 0
  • +

Replies To: Error: non-static method cannot be referenced from static context

#2 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Error: non-static method cannot be referenced from static context

Posted 08 October 2012 - 10:42 PM

It's because the Person class isn't static.

You are trying to use it like the Math class, which is.

EDIT: Forgot this. In order to use the methods in the way that you want, you have to use the object instead of the class.

This post has been edited by jdavi134: 08 October 2012 - 10:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,696
  • Joined: 19-March 11

Re: Error: non-static method cannot be referenced from static context

Posted 08 October 2012 - 10:44 PM

A static method is a method which can be called on the class itself: it pertains to no object, so it can't access any object's data, so you don't need an object to use it. A non-static method (an instance method) must be called on an instance of the class.

You need to call the birthday() method on an object of the Person class, not on the class generally.

In a way, what you're doing here is asking for the birthday for all people, when what you want is to ask for one particular person's birthday. Does that help?
Was This Post Helpful? 1
  • +
  • -

#4 holidaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-September 12

Re: Error: non-static method cannot be referenced from static context

Posted 08 October 2012 - 11:24 PM

View Postjon.kiparsky, on 08 October 2012 - 10:44 PM, said:

A static method is a method which can be called on the class itself: it pertains to no object, so it can't access any object's data, so you don't need an object to use it. A non-static method (an instance method) must be called on an instance of the class.

You need to call the birthday() method on an object of the Person class, not on the class generally.

In a way, what you're doing here is asking for the birthday for all people, when what you want is to ask for one particular person's birthday. Does that help?


I'm at the gym right now, so I haven't had a chance to test this, but wanted to reply with my ideas as its late and wanted to get input before people sleep.

Would it be something like...

Person.birthday(age);

Let me know if I'm off and I'll go back to brainstorming when I get home.

Thanks for the input.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,696
  • Joined: 19-March 11

Re: Error: non-static method cannot be referenced from static context

Posted 08 October 2012 - 11:26 PM

View Postholidaze, on 09 October 2012 - 01:24 AM, said:

View Postjon.kiparsky, on 08 October 2012 - 10:44 PM, said:

A static method is a method which can be called on the class itself: it pertains to no object, so it can't access any object's data, so you don't need an object to use it. A non-static method (an instance method) must be called on an instance of the class.

You need to call the birthday() method on an object of the Person class, not on the class generally.

In a way, what you're doing here is asking for the birthday for all people, when what you want is to ask for one particular person's birthday. Does that help?


I'm at the gym right now, so I haven't had a chance to test this, but wanted to reply with my ideas as its late and wanted to get input before people sleep.

Would it be something like...

Person.birthday(age);

Let me know if I'm off and I'll go back to brainstorming when I get home.

Thanks for the input.


Nope. You want a particular person. You've got one lying around to play with.
Was This Post Helpful? 0
  • +
  • -

#6 holidaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-September 12

Re: Error: non-static method cannot be referenced from static context

Posted 08 October 2012 - 11:32 PM

View Postjon.kiparsky, on 08 October 2012 - 11:26 PM, said:

View Postholidaze, on 09 October 2012 - 01:24 AM, said:

View Postjon.kiparsky, on 08 October 2012 - 10:44 PM, said:

A static method is a method which can be called on the class itself: it pertains to no object, so it can't access any object's data, so you don't need an object to use it. A non-static method (an instance method) must be called on an instance of the class.

You need to call the birthday() method on an object of the Person class, not on the class generally.

In a way, what you're doing here is asking for the birthday for all people, when what you want is to ask for one particular person's birthday. Does that help?


I'm at the gym right now, so I haven't had a chance to test this, but wanted to reply with my ideas as its late and wanted to get input before people sleep.

Would it be something like...

Person.birthday(age);

Let me know if I'm off and I'll go back to brainstorming when I get home.

Thanks for the input.


Nope. You want a particular person. You've got one lying around to play with.


Derp. I just got what you meant by if I use person than its like I'm calling the group and not the person object.

Using person1.birthday is correct I assume. Thanks for help, it's much appreciated.
Was This Post Helpful? 0
  • +
  • -

#7 holidaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-September 12

Re: Error: non-static method cannot be referenced from static context

Posted 09 October 2012 - 12:30 AM

I thought I got it, but it seems that the second person pane isn't showing the new values. Its supposed to say the following:

JPane 1 - Jake is 20 years old.
JPane 2 - Dr. Jake is 21 years old.

Any Ideas?

import javax.swing.JOptionPane;

   public class PersonCommand {
      public static void main(String[ ] args) {
		
	int age1 = Integer.parseInt(args[1]);
			
       	//creates one person objects
         Person person1 = new Person(args[0], age1);
      	
			//Prints the first JPane with relative data fields
			String display1 = person1.toString();
			JOptionPane.showMessageDialog(null, display1);
			
      	                //Calls phd and birthday method
                        person1.birthday();//Calls birthday method
                        person1.phd();//Calls phd method
                        JOptionPane.showMessageDialog(null, display1);

   	}//end of class
	}
/*Stores the name and age of the person*/
   class Person{
    //data fields that store each object's data
      private String name;
      private int age;
   	
   /** Constructor - Used To Create EAch Object & Initialize DAta Fields.
   * @param n1 is the Persons name 
   * @param a1 is the Persons age*/  
      public Person(String n1, int a1){
         name = n1;
         age = a1; 
      }
   	
   //Used to Display The Data Stored In EAch Object's DAta Field.
	public String toString(){
		String personString = name + " is " + age + " years old.";
		return personString;
	}
	//Method used to display age 	
	public void phd(){
		String n1 = "Dr. " + name;
		return;
	}
	//Method used to display age 	
	public void birthday(){
		Integer a1 = age + 1;
		return;
   }
  }
 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1