Java Nested Class Question

0 Developer John · February 9, 2015
Why isn't this valid when making a Java nested class:


public class Hello{

public static void main(String[] args){

class Test{
System.out.println();
}
}
}

-Thanks!8-)

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Nicholas Eason · February 9, 2015
The Class body can only contain methods and variable declarations/instantiations. This is because Java doesn't perfectly read the methods top to bottom. So if you say 
public static void main(String[] args){
potatoMethod();
}

System.out.print("Hello");

public static void otherMethod(){
//Whatever here
}
public static void potatoMethod(){
otherMethod();
}


You'll notice that the code will just skip over the print statement, this is because Java moves through the program via methods, not top-down/left-right. I intentionally set it up so you could see that at first it skips otherMethod, also but then goes back up. In fact, if you haven't already figured out, the program will crash because the print statement isn't in a method.
HOWEVER, saying: 


public static void main(String[] args){
potatoMethod();
}

int x = 1;

public static void otherMethod(){
System.out.print(x);
}
public static void potatoMethod(){
otherMethod();
}



X will print out properly. I believe this is because when you make a call to x, Java checks locally and through the class body (not other methods) for x, gets it's value, then returns the value. This might be wrong as to why it does that though.

To answer the other question, A nested class is just like any other class, except it's anonymous to anything other than that method/class it was declared in. It can have it's own methods, and variables, but it still have to follow the syntax.
0 Developer John · February 10, 2015
Isn't it because an object's state is stored in stored in its fields, while an object's behavior is stored in its methods? There should be a connection to that. 
0 Mathias Frits Rørvik · February 10, 2015
Never mind, I am mistaken. I was unaware that you actually can define a class within a method, but you can't call methods outside methods. I guess in short you could say that a class can only contain definitions. 
0 Developer John · February 10, 2015
Yes.
0 Duba Blaz · February 9, 2015
public class Hello{

public static void main(String[] args) {
        class Test  {
            private void method() {
                System.out.println("Hello");

            }
        }
        
        Test test = new Test();
        
        test.method();

}
}

Hi,

I think that you must put some methods there, so you can call it outside nested class as in example above. If there is no methods, "AnnotationName" compile error will appear.
0 Developer John · February 9, 2015
Oh wow, I can't believe I forgot to create a method. What kind of statements are acceptable inside the class body, but outside a constructor or method body? 
0 Duba Blaz · February 9, 2015
I think that it's not possible to add any statement inside nested class without methods. You can add variables, but to use for example System.out.println("Hello"); outside method will not work.
0 Developer John · February 9, 2015
Okay, so a class body can only contain fields that are variable declarations, but why?
0 Developer John · February 9, 2015
Wrong, classes can contain fields, constructors, methods, and nested classes.
0 Nicholas Eason · February 9, 2015
Constructors are a type of method, fields are variables, and the only one I left out is a Nested Class, because I thought it was quite obvious, seeing as that's what this question was about.
  • 1
  • 2

Java / Android Development

107,274 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator