I hop you up with a answer, it will take some time so keep on checking back in like an hour or so (or check all the saves i make because of formatting and stuff that drives me crazy..)
First of all your code does not compile the way you posted it. Be careful to tell the community it runs but it acctually doesnt. That mixed with dupplication of other posts make your question beeing downrated.
Here is a first version of your code with a very minor change to allow it to run:
public class PassReferenceTypeParamenterByValue {
public static void main (String []args){
PointForPassReferenceTypeParametersByValue p = new PointForPassReferenceTypeParametersByValue(2, 3);
System.out.println("The new X and Y postions of the point P after it has been moved by the method p.moveP() are X: " + moveP(p).xPosition + " and Y: " + moveP(p).yPosition + ".");
}
public static PointForPassReferenceTypeParametersByValue moveP(PointForPassReferenceTypeParametersByValue del_p){
del_p.xPosition=10;
del_p.yPosition=20;
del_p = new PointForPassReferenceTypeParametersByValue(del_p.xPosition, del_p.yPosition);
return del_p;
}
}
/* Make sure to understand the difference class types
*
* With "pulbic class...." you create a Top-Level class ("normal" java class) which has to reside in its own .java file
* So if you want to create a "public class MyClass" it MUST reside in a file MyClass.java.
*
* If you however want to define multiple classes whithin the same .java file what you need is a so called nested top level
* class. It is basicially the same like a top-level class except it is not stored within a own .java file with the same name
*/
class PointForPassReferenceTypeParametersByValue {
int xPosition;
int yPosition;
PointForPassReferenceTypeParametersByValue(int x, int y){
xPosition=x;
yPosition=y;
}
}
A first improvement to your code regarding readability could have be to rename your classes and methods to a sencefull length in characters so no one has to scroll horizontaly.
Now the second question is what you try to archive. I assume you just try to get a code where you have a Point with a x/y position and a method that can modify those coordinates. By above code beeing able to run now you got that.
Here however is a version id prefer over this following language independent concepts of the object oriented programming like encapsulation and stuff (those concepts are worth to read about and will then work in Java, .NET, C# and so on).
Alright what we do now is called refactoring, it will lead us to a more acceptable code which is in general better to understand, maintain and has a higher performance when beeing runned.
We do this step by step. First step is to follow basic rules of object oriented programming by creating a
better version of your previous implementation of something that is basicially a point.
public class PassReferenceTypeParamenterByValue {
public static void main (String []args){
MyPoint p = new MyPoint(2, 3);
//We already use the get/ set methods rather to "rape" your new MyPoint object by directly calling its member variables
System.out.println("The new X and Y postions of the point P after it has been moved by the method p.moveP() are X: " + moveP(p).getXPosition() + " and Y: " + moveP(p).getYPosition() + ".");
}
public static MyPoint moveP(MyPoint del_p){
del_p.setXPosition(10);
del_p.setYPosition(20);
del_p = new MyPoint(del_p.getXPosition(), del_p.getYPosition());
return del_p;
}
}
/* We call this MyPoint just because the sun is shining bright outside here in switzerland (teaching you how to name things properly
* exceeds the bounds of my answer right now.
*/
class MyPoint {
/* Encapsulation: You dont want other classes to directly access your class members (variables) so you define those
* as private. Thus to allow other classes to access those private members you provide get/set Methods. Go read about
* the java bean naming convention of bean propertys and the according get/ set methods right now and make sure to always
* use that naming pattern in the future (since tonns of frameworks youre maybe using in the future will rely on you following
* those standards
*/
private int xPosition;
private int yPosition;
MyPoint(int x, int y){
xPosition=x;
yPosition=y;
}
/* You dont want to have that for any variable in general but for those that should be accessable (indirectly) from
* outside your code.
*/
public void setXPosition(int xPosition){
/* Because you now have two variables (read about visibility of different variable types) with the same name you have
* to clearify which of both you mean. By adding 'this' you tell the compiler youre talking about the variable of the
* object rather than the local one of this method.
*/
this.xPosition = xPosition;
}
public int getXPosition(){
/* Here you dont need the 'this' because there is no other local variable with the same name, you can however always
* make that clearification so "return this.xPosition" is equal in that case.
*/
return xPosition;
}
//The same for the yPosition value:
public void setYPosition(int yPosition){
this.yPosition = yPosition;
}
public int getYPosition(){
//like told above 'return this.yPosition' is equal to 'return yPosition'
return this.yPosition;
}
}
In a second step we want to look at your Method that modifies a Point. There are two possibilitys what you want to archive here (well there are more but i go for two general ones) and thatfore make two methods:
public class PassReferenceTypeParamenterByValue {
public static void main (String []args){
MyPoint p = new MyPoint(2, 3);
System.out.println("The new X and Y postions of the point P after it has been moved by the method p.moveP() are X: " + moveThePointYouDeliverMe(p).getXPosition() + " and Y: " + moveThePointYouDeliverMe(p).getYPosition() + ".");
System.out.println("The new X and Y postions of the new point P after it has been created by the method p.moveP() are X: " + moveThePointYouDeliverMe(p).getXPosition() + " and Y: " + moveThePointYouDeliverMe(p).getYPosition() + ".");
}
/* If you just want to move a point and not create a new, independent (of the one delivered) one you
* can just use the set-methods of that point, modify its values and return the updated point.
* This will best match your above System-out which indicates you still have the same Point object you delivered to that method.
*/
public static MyPoint moveThePointYouDeliverMe(MyPoint del_p){
del_p.setXPosition(10);
del_p.setYPosition(20);
return del_p;
}
/* If you dont want to change the object delivered to your method but rather return a new, independent object
* your previous approach comes in with a little modification so you dont change the state of the delivered object
*/
public static MyPoint copyAndMoveThePointDeliveredMe(MyPoint del_p){
return new MyPoint(10, 20);
}
}
/* We call this MyPoint just because the sun is shining bright outside here in switzerland (teaching you how to name things properly
* exceeds the bounds of my answer right now.
*/
class MyPoint {
/* Encapsulation: You dont want other classes to directly access your class members (variables) so you define those
* as private. Thus to allow other classes to access those private members you provide get/set Methods. Go read about
* the java bean naming convention of bean propertys and the according get/ set methods right now and make sure to always
* use that naming pattern in the future (since tonns of frameworks youre maybe using in the future will rely on you following
* those standards
*/
private int xPosition;
private int yPosition;
MyPoint(int x, int y){
xPosition=x;
yPosition=y;
}
/* You dont want to have that for any variable in general but for those that should be accessable (indirectly) from
* outside your code.
*/
public void setXPosition(int xPosition){
/* Because you now have two variables (read about visibility of different variable types) with the same name you have
* to clearify which of both you mean. By adding 'this' you tell the compiler youre talking about the variable of the
* object rather than the local one of this method.
*/
this.xPosition = xPosition;
}
public int getXPosition(){
/* Here you dont need the 'this' because there is no other local variable with the same name, you can however always
* make that clearification so "return this.xPosition" is equal in that case.
*/
return xPosition;
}
//The same for the yPosition value:
public void setYPosition(int yPosition){
this.yPosition = yPosition;
}
public int getYPosition(){
//like told above 'return this.yPosition' is equal to 'return yPosition'
return this.yPosition;
}
}
Now we look at your code in a "generic" way because like this it sucks. Both methods just set static values but you want to allow others (which includes yourself in this case) to use that MyPoint class in a more generic way. So we allow them to tell us the new coordinates the point has to be moved to.
public class PassReferenceTypeParamenterByValue {
public static void main (String []args){
MyPoint p = new MyPoint(2, 3);
System.out.println("Created a Point with coordinates X="+p.getXPosition()+" , Y+"+p.getYPosition());
int newXPos = 20;
int newYPos = 10;
System.out.println("Moving the Point to the new coordinates X="+newXPos+" , Y="+newYPos);
/* Since you already have a reference 'p' to your point and know that your move-method wont change the reference (e.g. create and return a new
* Point Object. you can just call this method without storing the same reference:
*/
moveThePointYouDeliverMe(p, newXPos, newYPos);
System.out.println("The point was moved! New coordinates: X="+p.getXPosition()+" , Y+"+p.getYPosition());
}
/* We now allow the outerworld to tell us where to move that point to.
*/
public static MyPoint moveThePointYouDeliverMe(MyPoint del_p, int newXPosition, int newYPosition){
del_p.setXPosition(newXPosition);
del_p.setYPosition(newYPosition);
return del_p;
}
/* We dont need such a method because the outerworld can already create the same result by directly calling
* the constructor of MyPoint providing the values of x/y to the constructor
*
* So delte this comment and this method
*/
/*public static MyPoint copyAndMoveThePointDeliveredMe(MyPoint del_p, int newXPosition, int newYPosition){
return new MyPoint(newXPosition, newYPosition);
}*/
}
/* We call this MyPoint just because the sun is shining bright outside here in switzerland (teaching you how to name things properly
* exceeds the bounds of my answer right now.
*/
class MyPoint {
/* Encapsulation: You dont want other classes to directly access your class members (variables) so you define those
* as private. Thus to allow other classes to access those private members you provide get/set Methods. Go read about
* the java bean naming convention of bean propertys and the according get/ set methods right now and make sure to always
* use that naming pattern in the future (since tonns of frameworks youre maybe using in the future will rely on you following
* those standards
*/
private int xPosition;
private int yPosition;
MyPoint(int x, int y){
xPosition=x;
yPosition=y;
}
/* You dont want to have that for any variable in general but for those that should be accessable (indirectly) from
* outside your code.
*/
public void setXPosition(int xPosition){
/* Because you now have two variables (read about visibility of different variable types) with the same name you have
* to clearify which of both you mean. By adding 'this' you tell the compiler youre talking about the variable of the
* object rather than the local one of this method.
*/
this.xPosition = xPosition;
}
public int getXPosition(){
/* Here you dont need the 'this' because there is no other local variable with the same name, you can however always
* make that clearification so "return this.xPosition" is equal in that case.
*/
return xPosition;
}
//The same for the yPosition value:
public void setYPosition(int yPosition){
this.yPosition = yPosition;
}
public int getYPosition(){
//like told above 'return this.yPosition' is equal to 'return yPosition'
return this.yPosition;
}
}
Because the borring meeting with our bosses that have the same knowledge of software engineering like you just endet and i got to get back to work here is a final version with no comments. The last step makes your point provide a method to move itself rather than to having code that allows to move the point delivered (which is o.k. as well):
public class PassReferenceTypeParamenterByValue {
public static void main (String []args){
MyPoint p = new MyPoint(2, 3);
System.out.println("Created a Point with coordinates X="+p.getXPosition()+" , Y+"+p.getYPosition());
int newXPos = 20;
int newYPos = 10;
System.out.println("Moving the Point to the new coordinates X="+newXPos+" , Y="+newYPos);
p.moveMe(newXPos, newYPos);
System.out.println("The point was moved! New coordinates: X="+p.getXPosition()+" , Y+"+p.getYPosition());
}
}
class MyPoint {
private int xPosition;
private int yPosition;
MyPoint(int x, int y){
xPosition=x;
yPosition=y;
}
/* Like polite people polite programms ask things to move rather to just move them away not because the result differs
* but the way you got to the result :)
*/
public void moveMe(int newXPos, int newYPos){
/*We own those variables, we have the exclusive right to directly modify those values and are the only ones that dont
* need to call the set/get Methods for this
*/
this.xPosition = newXPos;
this.yPosition = newYPos;
}
public void setXPosition(int xPosition){
this.xPosition = xPosition;
}
public int getXPosition(){
return xPosition;
}
public void setYPosition(int yPosition){
this.yPosition = yPosition;
}
public int getYPosition(){
return this.yPosition;
}
}
I have absolutly no idear if this helps you, in case it does here is how i started to learn java. Its an easy way to get rid of the static context before you fully understand it and use it correctly:
public class PassReferenceTypeParamenterByValue {
public static void main (String []args){
new PassReferenceTypeParamenterByValue().myDynamicMain();
}
public void myDynamicMain(){
//Look at this as your new main for now until you understand the static parts in the java world.
System.out.println("From here on i dont have to care about static stuff...");
//Place your Code here
}
}
By the way this is how i learned java and become a quite acceptable software developper with 10 years+ experience in java (with a merely acceptable skill of english... no rly, sorry its horrible i know).
- I got one book with 589 pages that had a title like "Learning Java from beginner to professional".
- In the first two years after i became that book i continously tried things like you do and tried to fully understand why they work or why they dont
- After 2 years i have read that book over 20 times completly.
Now its time for me to go back to work and wish you the best of luck and fun learning java.