ClassName c = new ClassName(); In this statement you are creating (by declaring it) a variable on the left hand side. The name of the variable is c. It has a "type" of ClassName - which means that only things which can "prove" that they provide the same variables/methods as the class ClassName has can be referenced by this variable without a compiler error. On the right hand side you are instantiating the variable c. You are creating an object on the heap that will hold the "state" of the object (the current value of each of its varables) and putting a reference to it in the variable c. Notice that the "new" word is used to do the object creating. The "ClassName()" is a call to the constructor of the class ClassName. In this case the parenthesis do not hold any parameters so you are calling the default constructor which takes no parameters. The variable in this statement is a different thing than the object in this statement. Variables have a "type", objects have a "class". You can create a variable that is the "type" of an interface, and any object of any class that implements that interface can be referenced by that variable. However you can not actually create an object which is the "class" of an interface - because of course an interface is NOT a fully defined class. Map myMap = new HashMap(); The variable is of "type" Map. So since this line compiles, the thing that it is referencing must be either a class that implements Map, or a Sublass of Map (since it is obviously NOT the "same" type). You can't really tell which just by looking at this code. As it turns out Map is an interface, so HashMap must implement that interface.
"JavaRanch, where the deer and the Certified play" - David O'Meara
In essence you are correct. c will becomes an object of type ClassName. Because c is of type ClassName it has all the required variables and methods for ClassName. As for the "powerfulness": Seems to me that the best way to understand this type of programming is to try doing it without using an object.
Are you creating a new object of ClassName that "inherits" ClassNames' variables and methods? Where is the powerfulness in doing this kind of creation with objects? Thanks in advance. [/QB]
Hope that helps.
Joined: Sep 29, 2000
Yes, you are creating an object of class ClassName, and it will have all the fields (member variables)and methods() of ClassName. The REASON for creating an object is to track the status (current values) of "this" instance vs "that" instance. That is kept in the values of the variables of the instance. [ March 25, 2002: Message edited by: Cindy Glass ]