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 Developer John · February 10, 2015
Yes.
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
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 Developer John · February 9, 2015
Mathias, who are you in reference to? Please explain your comment. Secondly, I wanted to know if there were certain statements that could be declared within a class body. A variable declaration is a statement, but I wanted to know if there was any other valid statement. 
0 Mathias Frits Rørvik · February 9, 2015
You can't declare classes within a method.
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 Developer John · February 9, 2015
Oh yes, my question wasn't on how method references work when compiled.
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
Wrong, classes can contain fields, constructors, methods, and nested classes.
+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.
  • 1
  • 2

Java / Android Development

107,315 followers
About

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

Links
Moderators
Bucky Roberts Administrator