File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects" Watch "Objects" New topic
Author

Objects

Chris Stewart
Ranch Hand

Joined: Sep 29, 2001
Posts: 124
I hope this is the correct place for this question/discussion.
Can anyone help me understand the concept of creating a new object and such? Example, when you code

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.
Col Bell
Greenhorn

Joined: Mar 21, 2002
Posts: 12
This is one of those questions that isn't easy to answer meaningfully in just a few sentences. I would suggest going through the javasoft Java Tutorial http://java.sun.com/docs/books/tutorial/getStarted/index.html
A good book is Bruce Eckels "Thinking in java". This book doesn't just try to teach you java syntax but good design techniques for java as well. You can downaload a HTML version from here http://www.mindview.net/Books/TIJ/


Col<br /><a href="http://www.squirrelsql.org" target="_blank" rel="nofollow">http://www.squirrelsql.org</a>
Chris Stewart
Ranch Hand

Joined: Sep 29, 2001
Posts: 124
So would that code I posted create a new object?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Ronald Schindler
Ranch Hand

Joined: Jul 11, 2000
Posts: 50
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.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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 ]
Chris Stewart
Ranch Hand

Joined: Sep 29, 2001
Posts: 124
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects