| Author |
Pl explain this a bit diff code
|
jerry sharma
Greenhorn
Joined: Mar 30, 2006
Posts: 23
|
|
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 Q2 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
|
 |
Suhas Wadadekar
Ranch Hand
Joined: May 16, 2006
Posts: 95
|
|
Q1 My doubt is why not A option is true Q2 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 ]
|
 |
Gowher Naik
Ranch Hand
Joined: Feb 07, 2005
Posts: 643
|
|
Answer to question 2 There will be total 3 objects 1.Hello Ref(s1,s2) 2.pal(Lost) 3.Hellopal Ref(s3,s4)
|
 |
Gowher Naik
Ranch Hand
Joined: Feb 07, 2005
Posts: 643
|
|
For question 1 the option 1 can be incorrect in only one case if class is used as top level class,cause top level class cannot be static.If class is used as inner class then option 1 is correct.
|
 |
jerry sharma
Greenhorn
Joined: Mar 30, 2006
Posts: 23
|
|
Thanks to all friends for the expalination of Q2 i got it now Q1 is given as it is, not mentioned inner or something else reg classes so then what should be select regards jerry
|
 |
Gowher Naik
Ranch Hand
Joined: Feb 07, 2005
Posts: 643
|
|
|
if inner class is not mention then u should assume it as top level class.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
Q1. There are class (static) variables and instance variables. There is no such thing as a static instance variable.
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Asha Pathik
Ranch Hand
Joined: Feb 08, 2006
Posts: 143
|
|
Hi, 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.
|
SCJP 1.5
|
 |
Vijay Raj
Ranch Hand
Joined: Oct 10, 2005
Posts: 110
|
|
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. Do we get such questions come in the exam. regards, vijay.
|
 |
Irshad Nizami
Greenhorn
Joined: Jan 31, 2006
Posts: 4
|
|
|
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!
|
 |
 |
|
|
subject: Pl explain this a bit diff code
|
|
|