Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is a class and object?

 
YesYouretheOne
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
mfmz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mfmz,
Your name does not comply with the Javaranch naming guidelines which can be found here please register again with a valid name.
 
issu sri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic