Results 1 to 4 of 4
  1. #1
    Varuna is offline Member
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

    Question According to java why can not this program be compiled?

    public class Lab1
    {
    static Person pers;

    public static void main (String [] args){

    //Lab1.Person pers = new Lab1().new Person();
    pers=new Person();
    pers.setFirstName("Bob");
    pers.setLastName("Fields");
    pers.printOut();
    tester();
    pers.printOut();
    }

    static void tester(){
    Lab1.Person pers =new Lab1().new Person();
    pers.setFirstName("Homer");
    pers.setLastName("Simpson");
    pers.printOut();
    }

    class Person
    {
    String firstName;
    String lastName;


    public void setFirstName(String newFirstName)
    {
    firstName=newFirstName;

    }
    public String getFirstName()
    {
    return firstName;
    }
    public void setLastName(String newLastName)
    {
    lastName=newLastName;
    }
    public String getLastName(String newFirstName)
    {
    return lastName;
    }



    //Behavior
    public void printOut()
    {
    System.out.println("Name: " + firstName + " " +
    lastName );

    }
    }


    }

    The above program can not be compiled the error message is "non-static variable this cannot be referenced from a static context
    pers=new Person();" The problem I have in mind is pers is declared as an static variable and it is assigned an exact Person obect so even if it is an static variable it can find it's way to the to the Object, so even if things are this way according to my view why it can not be compiled

    Varuna:confused:

  2. #2
    asheesh_arora is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default Re: According to java why can not this program be compiled?

    Please visit forum4everyone.com to get the solution.
    Technical -> Java -> General

  3. #3
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by asheesh_arora View Post
    Please visit forum4everyone.com to get the solution.
    Technical -> Java -> General
    wtf? I'm working on the solution now... just give me a few minutes...

  4. #4
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    You already had the solution in there. Unless there is a reason you uncommented this line?
    Java Code:
    Lab1.Person pers = new Lab1().new Person();
    It worked when I changed the top part to this:
    Java Code:
    public class Lab1 {
        //static Person pers;
    
        public static void main(String[] args) {
    
            Lab1.Person pers = new Lab1().new Person();
            //pers = new Person();
            pers.setFirstName("Bob");
    
    ...

Similar Threads

  1. Java Program for doing FTP
    By Rajesh_J2EE in forum New To Java
    Replies: 1
    Last Post: 12-06-2008, 03:35 AM
  2. Using compiled Classes
    By Mindhunter74 in forum NetBeans
    Replies: 17
    Last Post: 12-02-2008, 10:15 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. New to Java Program
    By jvasilj1 in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 08:22 AM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •