Hi friends pl help me out to understand the following questions Q1 Which of the follwing is true about static modifier. A.static can be applied to : instance variables, methods, code Segments and classes. B.a static method cannot be overridden. C.inner classes can be both static & private. D.a static reference cannot be made through non static method or code block. E.abstract & static both can be applied together to a method.
Answer : B C D My doubt is why not A option is true
How many String objects are created when we run the following code. String s1,s2,s3,s4; s1 = "Hello";s2 = s1; s3 = s2 + "Pal";s4 = s3; A.1 B.2 C.3 D.4 E.0
How many String objects are created when we run the following code. String s1,s2,s3,s4; s1 = "Hello";s2 = s1; s3 = s2 + "Pal";s4 = s3; A.1 B.2 C.3 D.4 E.0[/QB]
For Q1, in my opinion, answer choice A is not correct because they have mentioned it can be applied to classes.. the staic modifier cannot be applied to classes, unless the class is inner (again not method-local). The answer choice simply says "classes" which certainly is not true.
For Q2 mu answer is Option C. First String object ---> "Hello" Second String object ---> "Pal" Third String object ---> "HelloPal"
pls anyone correct me if I am wrong... [ July 14, 2006: Message edited by: Suhas Wadadekar ]
D. a static reference cannot be made through non static method or code block.
I'm not able to understand the meaning of this statement. Does this mean that we cannot refer to the static variable from the non-static method of the class? If it so, then this is wrong because we easily refer to the static variables from both type of methods : static and non-static.
What kind of question is this. The words chosen to phrase the question are really confusing.
First, what is a static instance variable. Second, when the question says that static methods can be applied to classes, do we assume that teh question is only talking about top-level class or inner classes also. Thirdly, option D is wrong. Static variable or methods can be referenced from non-static methods. Only the opposite is not true as we do not have the 'this' reference in non-static methods.
Regarding first question, Static variables belong to the class and are initialized when the class is loaded at runtime. Hence, we can't apply 'static' modifier to instance variables which are initialized when a particular instance of a class gets created i.e. we can't make instance variables as class-specific, since their values may differ with each instance ! That's it!