Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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:

import javax.swing.JOptionPane;
public class Contact
{
private String street, city, state, zip, phone, email;

public Contact()
{
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()
{
setContact();

String address;
address = "";
address = street + ", " + city + ", " + state + ", " + zip;

return address;
}
}

Student - Code:

import javax.swing.JOptionPane;
import java.text.*;
/**
* 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
*/
public Student()
{
name = "noName";
gpa = 0.0;
address = null;
}//end 0-arg constructor

/**2-arg constructor
*
*/
public Student(String nameVal, double GPAVal)
{
setName(nameVal);
setGPA(GPAVal);
setContactInfo();
}//end 2-arg constructor

public void setName(String stuName)
{
name = stuName;
}//end setName

public void setGPA(double stuGpa)
{
gpa = stuGpa;
}//end setGpa

public void setContactInfo()
{
Contact.getAddress();
}

(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.
Please help.
[3088 byte] By [kmbweb13] at [2007-11-11 7:56:14]
# 1 Re: static message error from two non static classes
try contactInfo.getAddress() insteed of Contact.getAddress()
in setContactInfo() method.
becos getAddress is not static .
mr1yh1 at 2007-11-11 22:36:51 >
# 2 Re: static message error from two non static classes
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:

Contact myContact = new Contact();
myContact.getAddress();

Hope this helps.
destin at 2007-11-11 22:37:56 >
# 3 Re: static message error from two non static classes
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.
kmbweb13 at 2007-11-11 22:38:59 >