0

I would like to get a better understanding on how to reference objects in java. Given this code that will raise the error “error: non-static variable this cannot be referenced from a static context”:

public class Catalog {
    public class Student {
        public String name;        
        ...
        }
//Student

    public static void main (String[] args){
       Student s;
       s = new Student();
    }//main

Q1: Why does it work if class Student is defined in another file “Student.java”?

If I want to keep the Student class definition in the same file with main subroutine, the workaround found on the post “Non-static variable cannot be referenced from a static context” is:

public class Catalog {
public class Student {
    public String name;        
    ...  
    }

    public static void main (String[] args){
        oopExample prg;        
        prg = new oopExample();
        prg.run();    
    }//main

    Student std1;
    public void run(){

       std1 = new Student();
       std1.name = "Tobby";
    }
}//oopExample

Q2: Can you tell me if this is similar from the program execution point of view with case when I use the Student class definition in a separate file?

Q3: What would be the drawbacks of declaring Student as static? and simply use the following code:

public class Catalog {
    public static class Student {
        public String name;        
        ...
        }//Student

    public static void main (String[] args){
        Student s;
        s = new Student();
}//main

Thank you for your time,

Community
  • 1
  • 1
florin.iliescu
  • 302
  • 4
  • 10
  • 1
    Wait! The first code snippet doesn't work? – Chetan Kinger Jan 23 '17 at 09:09
  • 2
    Yeah, @CKing, `Student` here is a non-static inner class of `Catalog`, so it needs a `Catalog` instance before it can be instantiated. There's no such instance within the static `main` method, of course. – Dawood ibn Kareem Jan 23 '17 at 09:11
  • My point exactly. You know the answer right? Why do you still ask *Q1: Why does it work if class Student is defined in another file “Student.java”?* – Chetan Kinger Jan 23 '17 at 09:12
  • @DavidWallace I need to step back and apologize. I thought the comment from you was from the OP. Based on the facts presented in the question, the OP already knows the answer. I was being sarcastic with my first comment on this post. – Chetan Kinger Jan 23 '17 at 09:15
  • Oh, sorry, I thought it was addressed to me. Never mind. And I don't think OP does know the answer - they simply haven't bothered to look at all the very similar questions that have been asked before. – Dawood ibn Kareem Jan 23 '17 at 09:16

1 Answers1

0

Q1: Why does it work if class Student is defined in another file “Student.java”?

because the (only) class in a separate file is static implicitly.

Q2: Can you tell me if this is similar from the program execution point of view with case when I use the Student class definition in a separate file?

from the program execution point of view it does not matter (as long as you don't access something in class Catalog from class Student (which you should not do anyway, unless you have a good reason...)

Q3: What would be the drawbacks of declaring Student as static? and simply use the following code:

objects of the class Student cannot access properties of class Catalog which are not declares static.

But again: this is something you should do with care anyway.

Poornima
  • 174
  • 8
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51