-
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.
-
try contactInfo.getAddress() insteed of Contact.getAddress()
in setContactInfo() method.
becos getAddress is not static .
-
 Originally Posted by mr1yh1
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:
Code:
Contact myContact = new Contact();
myContact.getAddress();
Hope this helps.
-
Thanks
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.
Similar Threads
-
Replies: 2
Last Post: 08-27-2003, 11:00 PM
-
Replies: 6
Last Post: 03-04-2002, 03:36 AM
-
By David Rancour in forum Java
Replies: 1
Last Post: 05-11-2001, 09:32 AM
-
By Lim Wing Hoe in forum Java
Replies: 5
Last Post: 10-29-2000, 12:59 AM
-
Replies: 3
Last Post: 06-09-2000, 08:18 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks