This is a completely valid Java class declaration. An example of what MyClass var;
means in your code is this one:
Imagine this class is called Directory
(it represents a directory location in a hard drive in real life) and we want to simulate that a directory can have more (sub) directories (for simplicity, only one :) ). So we could simulate this using this structure:
class Directory {
int amountOfFolders;
Directory subdiretory; // <-- child object(directory)!
// Directory[] subdiretories; // out of curiosity, this would be much more real
}
This way when we create a Directory
object and set its child directory(ies) and do some stuff with it.
This is called Composition (read more at Difference between Inheritance and Composition and Inheritance and Composition (the "Composition’s Basics" part) )
See below code commented for some more details...
class MyClass {
int data;
// attribute of type MyClass, this can represent a hierarchical structure
// in real life applications.
MyClass var;
// this is not a class method, but its constructor, used to create instances
// of this class, i.e.: MyClass c = new MyClass();
public MyClass() {
}
// this is another constructor, which receives a parameter for setting its
// attribute data, i.e.: MyClass c = new MyClass(3);
public MyClass(int data) {
this.data = data;
}
}
Demo: https://ideone.com/2qirCQ
Here is the GitHub repository with this code.