Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Can You Instantiate a Class within its Definition?

Tags:

java

A coworker (who is very new to Java) stopped in today and asked what seemed like a very simple question. Unfortunately, I did an absolutely horrible job of trying to explain it to him. He had a book that had a little code that looked like this:

class XCopy {      public static void main(String[] args) {         XCopy x = new XCopy(); // 1         x.doIt();     }      public void doIt() {         // Some code...     } } 

He was confused on line 1. What he wanted to know was why a new instance of XCopy could be created within the definition of the class XCopy. He thought this would have given some sort of a forward referencing error. After all, we hadn't yet finished declaring what the class XCopy was, so how could we create one?

I certainly know that this is valid code but, when I tried to explain it to him, I found myself stumbling over the answer and I'm afraid I left him more confused than when he started. I'd like to hear some other explanations of why this works.

Any thoughts? Why can you instantiate an instance of a class within the definition of the class, itself?

like image 503
McGlone Avatar asked Mar 03 '11 20:03

McGlone


People also ask

Can you instantiate a class within itself?

Yes, you can, but it's relatively less common for you to want to. In a method like showLine() , which is an instance method, your code is already executing in the context of a Second_Example instance.

Why do you instantiate a class?

In Java, an OOP language, the object that is instantiated from a class is, confusingly enough, called a class instead of an object. In other words, using Java, a class is instantiated to create a specific class that is also an executable file that can run on a computer.

Can you instantiate a class within another class?

Yes you definitely can instantiate a class inside a constructor method of another class.

What does it mean to instantiate a class?

Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor.


2 Answers

You are defining the class, all its fields and methods etc at compile time. The instance is not created until runtime. So there is no contradiction, the class is fully defined by the time you reach line #1.

As others point out, because the main method is static you will reach line #1 without having instantiated an object, but you can do so without issue. I use this pattern all the time for one-class experiments.

like image 166
Matthew Gilliard Avatar answered Oct 09 '22 05:10

Matthew Gilliard


Because code is compiled first, and executed later. All the compiler needs to know to validate that line is that a class named XCopy exists, and that it has a no-argument constructor. It doesn't need to know everything about the class.

like image 40
Dave Costa Avatar answered Oct 09 '22 07:10

Dave Costa