public class Solution {
public static void main(String[] args) {
//write your code here
Red red = new Red();
red.Red();
Orange orange = new Orange();
orange.Orange();
Yellow yellow = new Yellow();
yellow.Yellow();
Green green = new Green();
green.Green();
Blue blue = new Blue();
blue.Blue();
Indigo indigo = new Indigo();
indigo.Indigo();
Violet violet = new Violet();
violet.Violet();
}
public static class Red {
public Red() {
System.out.println("Red");
}
}
public static class Orange {
public Orange() {
System.out.println("Orange");
}
}
public static class Yellow {
public Yellow() {
System.out.println("Yellow");
}
}
public static class Green {
public Green() {
System.out.println("Green");
}
}
public static class Blue {
public Blue() {
System.out.println("Blue");
}
}
public static class Indigo {
public Indigo() {
System.out.println("Indigo");
}
}
public static class Violet {
public Violet() {
System.out.println("Violet");
}
}
}
Can anyone help me
Resolved
Comments (14)
- Popular
- New
- Old
You must be signed in to leave a comment
Austin Okojie
9 June 2020, 11:49
The key is the printed string in the constructor, pass that string in to main
0
sourav nayek
22 May 2019, 22:56
red.Red(); not needed
0
Godwin
13 April 2019, 22:18
thanks rsingh04, that was helpful. But the question i have is how does the constructor display output when no method has been called?
0
Talha
8 August 2019, 14:41
You should read lesson 10 of lvl2. again
0
rsingh04
19 October 2018, 05:26
just remove the 2nd line of every objects you created.
+1
Thomas
20 May 2019, 22:28
A Rainbow ) ... still a little fuzzy but learning
0
hidden #10362262
15 October 2018, 14:43
I don't understand why the red.Red(); line is not required. I typed my code with the calls to the methods within each object and it failed. Yet when I commented out the lines such as red.Red(); it passed. Can someone please explain this? Thank you!
+2
Love Patel
28 November 2018, 04:53
its because the method you are trying to call is a constructor and they are called automatically when you create the object. you dont need to call a constructor with the (.) operator.
To recognize if the method of a class is a constructor check if the method name is the same as the class name? then its a constructor and it will be automatically called when you create an object of that class.
+15
Anant Parashar
7 June 2019, 08:08
Thanks a lot for the explanation!!
+1
Love Patel
28 August 2019, 23:19
I am glad it was helpful!
0
Klajddi Xhambazi
16 May 2020, 16:54
thank u buddy
0
Jacob Bradley
22 December 2020, 06:41
thanks...
0
Prahlad Singh
12 September 2018, 16:49
Thanks!
0
Khurram
12 September 2018, 16:17solution
calling the constructors explicitly is not required.
+6