static message error from two non static classes
I am new to java. 4 weeks to be exact but I'm having a problem with my project.
I have 3 classes. StudentLab(which has main), Student and Contact.
Contact - code:
public class Contact
private String street, city, state, zip, phone, email;
street = "";
city = "";
state = "";
zip = "";
phone = "";
email = "";
public void setContact()
String input = JOptionPane.showInputDialog("Enter the students street address:");
street = input;
String input1 = JOptionPane.showInputDialog("Enter the students city:");
city = input1;
String input2 = JOptionPane.showInputDialog("Enter the students state:");
state = input2;
String input3 = JOptionPane.showInputDialog("Enter the students zip");
zip = input3;
String input4 = JOptionPane.showInputDialog("Enter the students phone number:");
phone = input4;
String input5 = JOptionPane.showInputDialog("Enter the students email:");
email = input5;
public String getAddress()
address = "";
address = street + ", " + city + ", " + state + ", " + zip;
Student - Code:
* Define state of an Student Name and GPA
public class Student
// Define state of an employee with name and salary
private String name; //student name
private double gpa; // Student salary
private double fin, midt, q1, q2, q3, q4; //student grades
private Contact contactInfo = new Contact();
private String address;
/** 0-arg constructor
name = "noName";
gpa = 0.0;
address = null;
}//end 0-arg constructor
public Student(String nameVal, double GPAVal)
}//end 2-arg constructor
public void setName(String stuName)
name = stuName;
public void setGPA(double stuGpa)
gpa = stuGpa;
public void setContactInfo()
(if I put just getAddress() I get the error -"Student.java": cannot find symbol; symbol : method getAddress(), location: class Student at line 51, column 13
In the setContactInfo()
if i use getAddress() or setContact()or any method call from Student to Contact
I get the message
"Student.java": non-static method getAddress() cannot be referenced from a static context at line 51, column 21
the problem I have is neather class has any static methods in them. Due to requirements with the program I am not allowed to call the getAddress or setContact from the main method. It must come from the Student class.
I don't understand what I am doing wrong.
try contactInfo.getAddress() insteed of Contact.getAddress()
in setContactInfo() method.
becos getAddress is not static .
I will explain what mr1yh1 is trying to say. You can only access a method from a class preceeded by the class name if and only if the method is static. A good example is the Math class. All of the Math class's methods are static. Math.abs(var), Math.pow(var, var2), etc. What you need to do is create an instance of the Contact class:
Originally Posted by mr1yh1
Hope this helps.
Contact myContact = new Contact();
That worked and now i get it. I see what I was doing wrong.
I can now let my hair grow back in after pulling it out.
Last Post: 08-27-2003, 11:00 PM
Last Post: 03-04-2002, 03:36 AM
By David Rancour in forum Java
Last Post: 05-11-2001, 09:32 AM
By Lim Wing Hoe in forum Java
Last Post: 10-29-2000, 12:59 AM
Last Post: 06-09-2000, 08:18 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL