English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

if max is the name of my java class then what is the differencr between
max cal;
max cal = new max();
(are both the cal objects here)
also i had read(every object in a java class has its own copy of variable and methods what does this mean)
for eg in a same class say max i have made two objects say obj1 and obj2;
(if i have 3 var and 2 methods in the class then which object belongs to this variables and methods )
please help me clear this concept

2007-07-13 07:27:17 · 3 answers · asked by ask_the_genious 1 in Computers & Internet Programming & Design

3 answers

Keep in mind the object you are creating is suppose to represent something.
The variables describe what your object is and the methods allow you to interact with the object.

You created an object called max that has three variables and two methods. This tells us a little bit about max. I'm going to make up the variables and the methods so that it has meaning to the answer.

I'm going to say that max has the following three variables:

private String FirstName;
private String LastName;
private int Age;

And that max has the following two methods:

public String GetName(){
return this.FirstName + " " + this.LastName;
}

public void SetName(String fn,String ln){
this.FirstName = fn;
this.LastName = ln;
}


now when you said:
max cal;

you said that you knew you were going to create an object represented by max and it was going to be referenced by the name cal. You're not creating it yet. You're just getting it ready for when you are going to need it.

If you tried to set cal's name here by calling:
cal.SetName("Calvin","Hobbs");
you would get an error. Cal does not exist yet.

When you say:
max cal = new max();

you are saying that you need an object that is represented by max and will be referenced by the name cal. You are also saying you need it now and you want to create it.

If you tried to set cal's name here by calling:
cal.SetName("Calvin","Hobbs");
you would be just fine.

Now, to reference your question about " every object in a java class has its own copy of variable and methods what does this mean" by putting it into an example:

max cal = new max();
max cool = new max();

cal and cool both have access to the two methods.
Both also have the three variables.


so if you say:
cal.SetName("Calvin","Hobbs");
cool.SetName("Cool","Cat");


when you go to get cal's name it won't change because you set cool's name.

String calsName = cal.GetName();
String coolsName = cal.GetName();

sytem.out.println(calsName); // prints "Calvin Hobbs"
sytem.out.println(coolsName); // prints "Cool Cat"


I hope this has helped. If you need more clarification feel free to email me: irishtek@yahoo.com

2007-07-13 12:10:52 · answer #1 · answered by Anonymous · 0 0

The difference between 'max cal;' and 'max cal = new max();'
is that the first is not initilized and can not be used, the second is ready.

When you read that each has it's own variables and methods, they mean that each class has a lookup table to where it's methods and variables are stored.

Each variable is unique per object. So changing (non static) class variables in one class does not alter other objects.

Methods are unique in the class, but super classes can override the definition.

Consider the three classes below.

That the output is
a=1
b=0
c=0
a=0
b=1
c=0

When we passed the supermax variable to addone, it treated it as the superclass, but called the real objects method, nto the 'classes'.

Don't know if that helped or not =)

file mymax.java
package pkg;

public class mymax {
int a;
int b;
int c;

public mymax() {
super();
a = 0;
b = 0;
c = 0;
}

void inc() {
a++;
}

void dec() {
a--;
}

void print() {
System.out.print("a=");
System.out.println(a);
System.out.print("b=");
System.out.println(b);
System.out.print("c=");
System.out.println(c);
}
}

file supermax.java:
package pkg;


public class supermax extends mymax {

public supermax() {
super();
}

void inc() { b++ ; }
}

file usemax.java:
package pkg;
public class usemax {
/**
* @param args
*/
static void addone(mymax v) {
v.inc();
}

public static void main(String[] args) {
// TODO Auto-generated method stub
mymax cal = new mymax();
supermax sm = new supermax();

addone(cal);
cal.print();
addone(sm);
sm.print();
}

}

2007-07-13 15:26:14 · answer #2 · answered by Clif S 3 · 0 0

max cal;
This statement defines the variable cal as a type max. As yet the variable has not been instantiated it has just been defined as an object type.
max cal = new max();
This statement instantiates the variable cal as an object of type max. Cal now has a copy of any variables that are not of type class and any methods that are not of type class.
max data = new max();
This statement now instantiates another variable data as an object of type max. Data now has another copy of any variables that are not of type class and any methods that are not of type class.

I hope this helps.

2007-07-13 14:46:47 · answer #3 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers