From what I gather as a Java beginner, when accessing instance members, the "this" keyword may apparently be used.
see this program,

class sphere{
	static final double PI=3.142;
	static int count=0;
	double radius,xcentre,ycentre,zcentre;
	
	sphere(){
	xcentre=0.0;
	ycentre=0.0;
	zcentre=0.0;
	radius=1.0;
	count++;
	}
	
	sphere(double xcentre,double ycentre,double zcentre){
	this(); // what does this mean ??
	this.xcentre=xcentre;
	this.ycentre=ycentre;
	this.zcentre=zcentre;
	count++;
	}
	
	sphere(double r2, double x, double y, double z){
	this(x,y,z); // and what does this mean ??
	radius = r2;
	}
	
	public static int getcount(){
	return count;
	}
	
	double volume(){
	return ((4.0/3.0)*PI*radius*radius*radius);
	}
}

class SphereTest{
public static void main (String args[]){
	System.out.println ("Number of Objects: " + sphere.getcount());
	sphere ball = new sphere(4.0,1,1,1);
	System.out.println (ball.volume() + " " + sphere.getcount() );
	}

this(); what does this mean ??

and what does this mean this(x,y,z); also when i am running this program the object count is 2, why the count is 2 since i've created a single object?

this(...) calls another constructor for the same class.
So the constructor on line 15 starts by calling the default constructor (no args).
The constructor on line 22 starts by calling the constructor
sphere(double xcentre,double ycentre,double zcentre)
to deal with x,y,xcenter before going on the handle the extra r2 parameter

Since all the constructors increment count, when one constructor calls another they both update count.

do you mean x,y,z in line 23 become equals to the values xcentre , ycentre and zcentre??

but what does mean by this(); in line 15 ?? from where it is caling constructor ?

do you mean when the constructor on line 15 increment count then the constructor on line 22 also increment the count ??


and,
how this sphere(double r2, double x, double y, double z){ is equals to this sphere(double xcentre,double ycentre,double zcentre){ ???

because in sphere(double r2, double x, double y, double z) the first parameter is radius while in sphere(double xcentre,double ycentre,double zcentre){ the first parameter is xcentre ???

when i've created the object (line 39) sphere ball = new sphere(4.0,1,1,1); the first value i passed is 4.0, is it a xcentre or radius ??

The x,y,z on line 23 are the parameters passed into that constructor (line 22). this(x,y,z) on line 23 just calls the constructor on line 14.

Calling this() from inside a constructor calls the sphere() constructor (line 6).

This happens when you have >1 constructor and don't want to duplicate the code from one to the other. You can't call Sphere() because that will create a second new Sphere. By referring to it as this() you run the code in that constructor without actually creating a second new object.

this() basically referred to the constructor
or we can say in ur program you have been used same variable name in the class and in constructor too so to defferiantiate these variables this key word is used which indicate the class variable catch the value whatever u pass in constructor argument....

commented: i wasn't asking this :) +0

The x,y,z on line 23 are the parameters passed into that constructor (line 22). this(x,y,z) on line 23 just calls the constructor on line 14.

Calling this() from inside a constructor calls the sphere() constructor (line 6).

This happens when you have >1 constructor and don't want to duplicate the code from one to the other. You can't call Sphere() because that will create a second new Sphere. By referring to it as this() you run the code in that constructor without actually creating a second new object.

Ok this(x,y,z) on line 23 calls the constructor of line 14 , ? but what are the values of these x,y,z ???

and please answer my second question i asked in my last post

1. The x,y,z on line 23 are the parameters passed into that constructor (line 22). Their values are whatever the calling code passed as parameters when it called this constructor.

I answered before you added the second q, so I didn't see it.
The compiler knows which constructor to use because they all have different numbers and/or types of parameters.
new sphere(4.0,1,1,1) has 4 parameters so it is a call to the constructor on line 22 because that's the one with exactly 4 parameters.
this(x,y,z) refers to the constructor on line 14 because that's the one with 3 parameters.

commented: ++++++++= +0

1. The x,y,z on line 23 are the parameters passed into that constructor (line 22). Their values are whatever the calling code passed as parameters when it called this constructor.

I answered before you added the second q, so I didn't see it.
The compiler knows which constructor to use because they all have different numbers and/or types of parameters.
new sphere(4.0,1,1,1) has 4 parameters so it is a call to the constructor on line 22 because that's the one with exactly 4 parameters.
this(x,y,z) refers to the constructor on line 14 because that's the one with 3 parameters.

so (x,y,z) equals to the parameters of line 14 constructors ???
x=xcentre ??
y=ycentre ??
z=zcentre ??

Other way round. x,y,z are parameters in the constructor on line 22. They have the values passed in when that constructor is called.
On line 23 those values are passed to the constructor on line 14. In the definition of that method the parameters are named xcentre ycentre zcentre, so
xcentre in the line 14 constructor is a copy of x on line 23
etc
It's exactly the same as passing parameters to any ordinary method.

so why there is need to create a constructor of line 23 since we already have a constructor in line 14 ??

and should i remove count++; from the line 14 constructor ?? since it is already present in line 6 constructor ?

u r confused with this()
it is just as you use same variable name xcenter,ycenter,zcenter in class and in constructor parameter too so thats why to differentiate b/w theese variable name this() has been used now in line 24 you use radius and r2 as variable name so assign value to this variable....

so why there is need to create a constructor of line 23 since we already have a constructor in line 14 ??

Because they take different parameters. Depending on how many of the variables you want to set when you create a new instance, you call the constructor with the appropriate parameters.

and should i remove count++; from the line 14 constructor ?? since it is already present in line 6 constructor ?

Good question. count holds the number of instances created, so count++ must be called once and once only for each time an instance is created. When one constructor calls another this can be tricky to guarantee. You just have to look at how they can call each other and work out the best place to put it.

from line 6 to 12 its a default constructor which call ed at time of making object so count inceremented by 1 then in next constructor in which parameter has been passed n agin count incremented so therefore it returns 2...

from line 6 to 12 its a default constructor which call ed at time of making object so count inceremented by 1 then in next constructor in which parameter has been passed n agin count incremented so therefore it returns 2...

We already covered this point 6 days ago. Please check the previous posts before posting. Your contribution is very welcome, as long as it adds something new.

oh h i m sorry....bt i m jst helping n i hv cheked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.