static message error from two non static classes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: static message error from two non static classes

  1. #1
    Join Date
    Feb 2006
    Posts
    2

    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.

  2. #2
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    try contactInfo.getAddress() insteed of Contact.getAddress()
    in setContactInfo() method.
    becos getAddress is not static .

  3. #3
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote 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.

  4. #4
    Join Date
    Feb 2006
    Posts
    2

    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

  1. Static variables and inheritance
    By grmAbay in forum .NET
    Replies: 2
    Last Post: 08-27-2003, 11:00 PM
  2. Serialise Modules or Static Classes?
    By Vinny in forum .NET
    Replies: 6
    Last Post: 03-04-2002, 03:36 AM
  3. Static methods question
    By David Rancour in forum Java
    Replies: 1
    Last Post: 05-11-2001, 09:32 AM
  4. consequences of using static variables?
    By Lim Wing Hoe in forum Java
    Replies: 5
    Last Post: 10-29-2000, 12:59 AM
  5. private and static
    By Mark in forum Java
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center