Skip to main content

Newbie help wanted

2 replies [Last post]
mikemc1
Offline
Joined: 2010-03-02
Points: 0

I am in the process of teaching myself Java. I am using both the Sun web-site tutorials and what my job offers. I have a question, what is the difference between an Object and a Class? I looked on Wikianswers but unfortunately I don't understand their answer (at this point like reading Greek).

I know a class contains variables and methods, so is an object the same thing?

I know this forum is for advanced people / professions who already have masterd this stuff so bear with me.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
unikdev
Offline
Joined: 2010-03-29
Points: 0

Hi,

I am also teaching myself Java and I will be glad to help whenever you migth need it.
To answer your post , a class is like a template or blueprint if you prefer that enables you to create many objects that have the same attributes and behaviour but with different state.

An example :

//here you have a class student

public class Student {
String name;
int age;

public Student(String theName,int theAge) {
name=theName;
age=theAge;
}

public void setName (String newName) {
name=newName;
}

}

//here i create student objects

Student student1= new Student("John",20);
Student student2= new Student("Marie",30);

Both are objects created from the Student class; they have the same attributes name and age; the same behavior since can do the same thing with the setName method ; but they have different states since the value of the attributes differ.

Hope this was helpful
Cheers

fervincent
Offline
Joined: 2007-11-23
Points: 0

Hello,
For example this is a class:

class Dog
{
int age;
String color;
String name;
public void run(){

}
}

An object Dog is an instance of that class. The class Dog helps you to create many objects.

Dog fifi=new Dog();
fifi.age=10;
fifi.color = "yellow";
fifi.name = "fifi";

Dog marvella=new Dog();
marvella.age=10;
marvella.color = "yellow";
marvella.name = "fifi";

In summary in this example we have one Class called Dog, and two objects of that class: fifi and marvella.