wood burning stoves*
The moose likes Beginning Java and the fly likes What is a class and object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is a class and object?" Watch "What is a class and object?" New topic
Author

What is a class and object?

YesYouretheOne
Greenhorn

Joined: Jan 17, 2001
Posts: 3
May I know what are the two, I thought they are 1 and the same.
If we have class and object, why do we still need the dot operator, isn't it redundant?
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
I see it like this ... a class is the byte code compiled from the source file a programmer writes. An object is an instance of that class ... many objects can come from one class.for example consider this.
String hello = "Hello";
String goodbye = "Goodbye";
Here there are two Objects each coming from the String Class!

Also your name does not comply with the Javaranch naming guidlines which can be found here please register again with a valid name.
[This message has been edited by Angela Poynton (edited January 17, 2001).]


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
mfmz
Greenhorn

Joined: Sep 11, 2000
Posts: 7
Actually class is a u can say Model/Prototype Definition etc..
and to use this prototype/modem u have to Create Object (Instance) of it..
or u can say when u define a class u actually define a "New Data Type"... and obviously to use that datatype u have to create a variable of it...
Class MyClass { } (Class/New Data type definition)
and to use this Datatype..
MyClass m=new MyClass() (where m is a variable/object/instance of type MyClass)..
hope u got your reply
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
A class is a type. Think of it as a classification. A class doesn't take up any memory. An object is an instance of that type, and it takes up memory.
We can create a class of type Mammal.
public class Mammal {
}
A lot of things can be a Mammal - you, me, a dog, etc.
When we allocate memory to that type, we're create an object in memory that represents that type.
Mammal you = new Mammal();
Mammal me = new Mammal();
Mammal dog = new Mammal();
-Peter
Marius Holm
Ranch Hand

Joined: Sep 11, 2000
Posts: 84
Peter Tran,
You say a class does not take up memory. But where is the class stored? In the file? But the file loads in memory as well?
Regards,
Marius
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
A class is collection. It does not exist, however, it has many things under it.
For example,
public class SoftDrinks
//SoftDrinks is a class
//It is a collection of pepsi, cook, etc.
There is a very good tutorial that might help you to understand class and object. http://sepwww.standford.edu
Hope I can help more. Thanks.
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Marius,
I totally agree with you. A class once loaded from a file takes up phsical memory, but this memory is some area used by the JVM. From the perspective of your application, a class is type. I'm not sure it's possible for you to get access to where the JVM stores the information about your class. When you create an object of that class type with the keyword new, then you're allocating memory also somewhere in the JVM. The difference is you can get access to this memory (i.e. object) via the reference handle of that class or its super class.
Is there anyone out there who can help us clarify this issue? Is it possible to get access to the memory used by the JVM to store the class when it's loaded into memory?
Thanks,
-Peter
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

mfmz,
Your name does not comply with the Javaranch naming guidelines which can be found here please register again with a valid name.


SCJP
Visit my download page
issu sri
Greenhorn

Joined: Jan 25, 2001
Posts: 2
Hello peter tran,
Ur explanation abt classes & objects are O.K.
I'm not sure abt JVM .
But in C++ , Class is a collection.It's similar to Structures of C.As u said classes need objects (i.e.)instances to save data.
regards,
issu sri.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: What is a class and object?