Reputation: 1037
public class Neat {
public Neat asdf;
}
does this make an object of the class Neat? I'm currently trying to learn Linked Lists, if that helps.
I've no idea how to Google this, sorry.
Upvotes: 2
Views: 110
Reputation: 85
public class Neat {
public Neat asdf; //Focus here
public static void main(String...string){
Neat n = new Neat();
}
}
No,It does not create an object, its is just a variable having potential to have object of Neat class but
public class Neat {
public Neat asdf = new Neat(); //Focus here
public static void main(String...string){
Neat n = new Neat();
}
}
It will make as many object of Neat and eventually result in StackOverFlow...
Upvotes: 0
Reputation: 10810
This is a class with a member variable that is of the same type as the class. This is perfectly legal in this case, however it can be dangerous. For example, if you change your code to merely instantiate the variable
public Neat asdf = new Neat();
Then create a Neat
elsewhere, your program will crash with a StackOverflowError
because each instance creates a new instance, which creates a new instance, and so on. As it stands, your program creates a reference variable, but not an instance, so no further instances are created.
Upvotes: 0
Reputation: 11841
This creates a class namedNeat
that declares a member variable named asdf
of type Neat
. The member variable asdf
is a reference to null
.
This code, as is, does not create an instance of an object Neat
Upvotes: 3
Reputation: 11911
This declare a field of type Neat
. So the Neat object will have a children Neat
field.
Neat n = new Neat();
n.asdf = new Neat();
Upvotes: 0