Constructor Questions 2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Constructor Questions 2

Hi evryone! I'd like ask something How can i also a default value also grass not only sky? And how know that case(multiple variable) v2 that which variable will be green? Thank you

23rd Mar 2018, 2:10 PM
Erik Horvath
Erik Horvath - avatar
10 Answers
+ 5
The tag is used so when people do searches for posts it'll assist with generating appropriate results. Here is your code with some edits from me: (Let me know if you need me to explain further) https://code.sololearn.com/cvTpdrjVW2B6/#java public class Earth { private String sky; private String grass; Earth() { this.sky = "Red"; this.grass = "Green"; } Earth(String sky, String grass) { this.sky = sky; this.grass = grass; } // Setters public void setSky(String sky) { this.sky = sky; } public void setGrass(String grass){ this.grass = grass; } // Getters public String getSky() { return this.sky; } public String getGrass() { return this.grass; } } public class Program { public static void main(String[] args) { //color will be "Red" for Sky, "Green" for Grass Earth v1 = new Earth(); //We will set whatever color we want Earth v2 = new Earth("Blue", "Yellow"); // Lets display our values System.out.println("V1's sky is " + v1.getSky() + " and grass is " + v1.getGrass()); System.out.println("V2's sky is " + v2.getSky() + " and grass is " + v2.getGrass()); // Lets use our setters to change values v1.setSky("Pink"); v1.setGrass("Purple"); v2.setSky("Orange"); v2.setGrass("Black"); // Print new values // Lets display our values System.out.println("V1's sky is " + v1.getSky() + " and grass is " + v1.getGrass()); System.out.println("V2's sky is " + v2.getSky() + " and grass is " + v2.getGrass()); } } ::: OUTPUT ::: V1's sky is Red and grass is Green V2's sky is Blue and grass is Yellow V1's sky is Pink and grass is Purple V2's sky is Orange and grass is Black
23rd Mar 2018, 2:51 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
If you could, post a link to your code. The link that you posted as a tag doesn't take me to any code but redirects back to Code Playground. Post up link and I'll take a look at it for you. You can create a default constructor, as well as multiple constructors with parameters, to determine how your initial object will be once created. While I wait for your response, I'll create an example for you.
23rd Mar 2018, 2:14 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Here is an example. Sky is default when object is created, but you have option to set its type upon creation or change it afterwards. Hope this helps. https://code.sololearn.com/c9h5TlfzJcEF/#java class myClass { private String type; // Default constructor public myClass(){ this.type = "Sky"; // default will be Sky } // Another constructor - takes type as an argument public myClass(String type){ this.type = type; } // setters public void setType(String type){ this.type = type; } // getters public String getType(){ return this.type; } } public class Program { public static void main(String[] args) { // Create objects from our class myClass obj1 = new myClass(); myClass obj2 = new myClass("Grass"); // Print object's type member System.out.println("Obj1 Type is " + obj1.getType()); System.out.println("Obj2 Type is " + obj2.getType()); // Use setter to change type obj1.setType("Dirt"); obj2.setType("Sand"); // Print objects again to see changes System.out.println("Obj1 Type is now " + obj1.getType()); System.out.println("Obj2 Type is now " + obj2.getType()); } }
23rd Mar 2018, 2:23 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
@Erik Reference the second set of code I posted to you: https://code.sololearn.com/cvTpdrjVW2B6/#java It's same concept, you're just changing the name of your variables is all. You would change leg, hand, etc.. in the same way that we were changing sky, grass, etc.. Replace Earth with Body, replace Sky with hand, replace Grass with leg. Then just make sure your variables match up throughout the code.
23rd Mar 2018, 4:09 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
@Erik You're welcome, Erik. Earth() { this.setSky("Red"); } ^In this example, you're using one of the setter methods to change your value to red. Earth() { this.sky="Red"; } ^This is setting the member's value directly without calling the method. The end result would be the same, but how you're reaching the end is different. Since you're doing all of this from within the class itself, I prefer to directly set the variable since it's within scope, rather than calling a function and then doing exactly what I was doing. If it was outside of the class/scope, then that's when I use the setter method. This is more about consistency with what I program, but also about doing less things to reach the same thing, which often equates to better performance. However, in this example, if there is any type of performance gain, it's negligible gain. As for the other section of code, the Earth() {} is the constructor, so you can set ALL of the initial values for your object's members. As such, any members of the class which you want to have a specific default values should be specified here. When you create an object, this portion of code is executed automatically and the member values will be set at that point. You can use parameters/arguments so that you can specify those values yourself from the code that you create the object at, which is where the other constructors I posted come into play. Hope that makes sense. Let me know if I should explain it further.
23rd Mar 2018, 5:16 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
What purpose the "tag" ?( I cant post anything while this empty.) Here is my code the URL: https://code.sololearn.com/cbb5ulCcgSYQ/#java Thank you
23rd Mar 2018, 2:27 PM
Erik Horvath
Erik Horvath - avatar
+ 2
Thank you your answer and your efforts. 1 But i would like know how can i change John and Jack not hand but leg. 2 And know what decide what will be change leg or hand from these: John = new Body(); and this Body Jack = new Body("black"); thanks very much Here is my code to metaphor class Body { private String hand; private String leg; // Default constructor public Body(){ this.hand = "white"; // default will be Sky } // Another constructor - takes type as an argument public Body(String hand){ this.hand = hand; } // setters public void setHand(String hand){ this.hand = hand; } // getters public String getHand(){ return this.hand; } } public class Program { public static void main(String[] args) { // Create objects from our class Body John = new Body(); Body Jack = new Body("black"); // Print object's type member System.out.println("John hand is " + John.getHand()); System.out.println("Jack hand is " + Jack.getHand()); // Use setter to change type John.setHand("yellow"); Jack.setHand("brown"); // Print objects again to see changes System.out.println("John hand is now " + John.getHand()); System.out.println("Jack hand is now " +Jack.getHand()); } }
23rd Mar 2018, 3:40 PM
Erik Horvath
Erik Horvath - avatar
+ 2
Thank you very much and what is difference bettwen Earth() { setSky("Red"); } or even Earth() { sky="Red"; } and that solution that you use Earth() { this.sky = "Red"; this.grass = "Green"; } Thank you
23rd Mar 2018, 4:23 PM
Erik Horvath
Erik Horvath - avatar
+ 2
You are welcome!;-]
23rd Mar 2018, 7:07 PM
dieserrapha
dieserrapha - avatar
+ 1
@ Jakob Marley Thank you. It's very usefull I'd like ask that what is the diffferent: Earth() { this.sky = "Red"; } and Earth() { setSky("Red"); } so without "this" somebody said me that i mustn't use "this." thank you
23rd Mar 2018, 6:49 PM
Erik Horvath
Erik Horvath - avatar