Roy G. Biv…

  • 3
  • unavailable
In all ages, the rainbow has inspired artists and poets and served as a good omen for ordinary people. Programmers are also people capable of admiring the beauty of nature. Since we're talking about the aesthetics of the rainbow, let's create 7 objects. Each of them will display the name of one of the colors of the rainbow.
You can't complete this task, because you're not signed in.
Comments(15)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Vaibhavi Level 4 , Vijayawada
28 November, 12:25
Please clarifz the code , I did this way but still facing Issues on the same
Muhammad VahhaajLevel 19 , Rawalpindi
4 June, 19:21
Just: new Red(); new Orange(); creates new objects and if you are guessing the automatic output it's because of default constructors(println in constructors)
pravinLevel 4 , Nagpur
30 May, 05:49
public class Solution { public static void main(String[] args) { //write your code here Red red = new Red(); } public static class Red { public Red() { System.out.println("Red"); } } } output of these program is red i want ask you that how object call the method.
LuyiLevel 27 , Kosice
6 May, 09:10
maybe it help for someone: https://stackoverflow.com/questions/7486012/static-classes-in-java // I have found it useful
DerekLevel 11 , Apex, NC
18 March, 19:08
my initial thought was along the lines of Red red = new Red(); red.Red(); but this didn't work. Syed's example below works, but I don't know why. Seems the Red() method within the class is being called automatically without being explicitly called. Any explanation why?
Jesús de LleraLevel 23 , Badajoz
25 March, 20:20
You are actually calling the method inside the class (it's called constructor method) when you type 'new' + 'name of the class'. For instance, new Red() calls the constructor method 'public Red()'.
Syed Tayyab ul MazharLevel 11 , Karachi
1 January, 17:47
Red red = new Red(); Orange orange = new Orange();
MasoodLevel 6 , Karachi
31 August 2018, 07:50
Getting the following error. "The order in which the objects are created must correspond to the order of the colors in the rainbow." Everything was perfect. Even the order. What is wrong with Codegym? Codegym please help!
RomanLevel 41
31 August 2018, 12:56
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
MasoodLevel 6 , Karachi
31 August 2018, 14:51
Thank you Roman! I'll do that.
GhanakiranLevel 4 , Vellore
28 August 2018, 06:18
I created object, but i cant Directly call the method inside the class Red to display the required color. why? public class Solution { public static void main(String[] args) { //write your code here Solution solution1 = new Solution(); solution1.Red(); Solution solution2 = new Solution(); solution2.Orange(); Solution solution3 = new Solution(); solution3.Yellow(); Solution solution4 = new Solution(); solution4.Green(); Solution solution5 = new Solution(); solution5.Blue(); Solution solution6 = new Solution(); solution6.Indigo(); Solution solution7 = new Solution(); solution7.Violet(); } public static class Red { public Red() { System.out.println("Red"); code coninues........ ...... ...... ......
28 August 2018, 13:49
Try this:
public class Solution {
    public static void main(String[] args) {
        //write your code here
        Solution solution1 = new Solution();
        solution1.Red();
    }

    public void Red() {
        System.out.println("Red");
    }
}
28 August 2018, 13:51
Or this:
public class Solution {
    public static void main(String[] args) {
        new Red();
    }

    public static class Red {
        public Red() {
            System.out.println("Red");
        }
    }
}
28 August 2018, 13:53
Also, you can create a question in the "Help" section and show your code there.
MasoodLevel 6 , Karachi
31 August 2018, 08:01
You can not call the static nested classes directly from the outer class. Because static nested classes are actually members of the outer class. And the static nested class can not access the fields and methods of the outer class directly. In order to access the static nested class from the outer class. First create instance of the static nested class in the outer class like this, OuterClass.staticNestedClass nestedObject = new ........ and so on. Best of luck