0

How to display a variable belonging to a class in a constructor.

Here is the code; private class House { Public String color; House (){ color="red"; } } public static void main (String []args){ House r= new House (); /**when I run this code, there is no output. How do I display "red" from this code. At first I wrote " House.r ();" but it still shows no output. If someone could explain and give me the solution. I'll appreciate it. */

23rd Jan 2017, 6:17 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
2 Antworten
0
I think you have to use the get command public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); v1.setColor("Red"); System.out.println(v1.getColor()); } //Outputs "Red"
23rd Jan 2017, 7:16 PM
Jason Inman
Jason Inman - avatar
0
Thank you but the code you giving me is referring to a constructor that takes parameters. I am looking to display a code a constructor without a parameter whereby the color is red. I appreciate the reply though.
24th Jan 2017, 6:16 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar