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
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?
+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 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.
0 Developer John · February 9, 2015
Oh yes, my question wasn't on how method references work when compiled.
0 Nicholas Eason · February 9, 2015
You said: "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?"
And: "Okay, so a class body can only contain fields that are variable declarations, but why?"


Your questions definitely resembled that question, along with an example of a print statement not inside a method. I simply gave a visual example along with the worded explanation because I don't know how you learn, so to attempt to get my point across in one post, I try to include visual examples.
0 Mathias Frits Rørvik · February 9, 2015
You can't declare classes within a method.
  • 1
  • 2

Java / Android Development

107,182 followers
About

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

Links
Moderators
Bucky Roberts Administrator