Menu
Topics Index
...
`

Flowergarden


If you need explanation Read this topic

If you need Answer Take test on this topic

Flowergarden
What will be the output of the following program?
public class Flower {
    public enum MyFlower {
        LILLY(0xff0000), ROSE(0x00ff00), JASMINE(0x0000ff);
        private static int flr;
        MyFlower(int flr) {
            MyFlower.flr = flr;
        }
        public int getLRJ() {
            return flr;
        }
    };
    public static void main(String[] args) {
        MyFlower flr = MyFlower.ROSE;
        MyFlower flr1 = MyFlower.JASMINE;
        MyFlower flr2 = MyFlower.LILLY;
        System.out.print(flr+",");
        System.out.print(flr1+",");
        System.out.print(flr2);
    }
}

A. ROSE,JASMINE
B. ROSE,JASMINE,LILLY
C. ROSE,
D. Some other code
E. Compilation Error or Runtime Error
Topic: Java Enumeration

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is e bcz in MyFlower(int )  MyFlower.flr=flr;  MyFlower.flr leads to illegal reference to static field. So compilation error occurs

Posted by Pooja Singh    2014-05-09 08:54:15


ans E because the reference which is given to static field is not legal from intializer MyFlower.flr=flr;

Posted by Manasa Jyothi Sree    2014-05-09 09:01:07


Compiler throws an error here because we cannot reference a static field of an enum type that is not a compile-time constant from constructors. This problem arises due to the execution order: With enums, the enum values are initialized before anything else. To initialize them, constructors must be called. The first constructor (at the time of construction of LILLY) references a static field (LINE 6) that may not have been initialized at the time it is called. That's the reason why this code will not compile. So, the answer is E.

Posted by Shaileshwar Sharma    2014-05-09 09:31:20


Congratulations Pooja Singh. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-12 04:22:59


© meritcampus 2019

All Rights Reserved.

Open In App