• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

question from JvalTest

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 54 from JvalTest:
Which of the following 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.
Correct Answer is B, C and D.
But I think the correct answer is A, B, C and E.
A is correct because applying static to an instance variables make it become static variables and there's static method, static initializer block and nested class.
D is wrong because you can always refer to a static variables from an instance method.
E is correct because interface method is of public, static, abstract
Correct me If I am wrong.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wong KK
This is a great Question. Unfortunately I will have to restudy my notes to answer all the question.
Answer E is wrong because applying "abstract" to a method requires that you "HAVE TO" subclass that method.
The nature of "static" is that it is related to a "class" and only 1 instance of it exists (therefore you cannot override it, which is what "abstract" requires).
Hope this short answer clarifies why option E is wrong.
I will be keeping a regular check to see why the rest are correct/incorrect.
Zahid.
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
How do apply static to CODE SEGMENTS???
Up to what I think, this is not possible.
Axel
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just to clarify
I think A) is wrong.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody,
"A" is not correct as when we say classes, we mean top level classes, while in JAVA top level classes can not be static only inner classes can be static.
"B" & "C" are correct as already known.
"D" is correct because u can not make a static reference from a non static block, unless u make an instance.
"E" is wrong, as abstarct methods have to be overridden and static methods can not be overridden. So, u can give both the modifiers to the same method.
And for u Axel, we can make a static block of code as static. and that is called Static Initializer ( refer to page 87 of Rberts and Heller ).
I hope this will work for all.

 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL
Axel, to make a static block static you can do the following:
class explainStatic {
static{
System.out.println("this will show once the class is loaded at class load time");
}
}
The code enclosed inside the curly braces is executed once the class is loaded.
As far as I am aware it cannot be accessed by any Object. It is not associated to any instance (Object) of the class ExplainStatic.
Hope this is clearer for you.
For all my Seniors out there, my question is " does the static code get recalled each time you create a new object of the explainStatic Class " ???
A prompt answer would be Appreciated.
Zahid.

[This message has been edited by Zahid, Butt (edited March 20, 2001).]
 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Axel,
You can apply static to code segments using static initializers as in.
class InitializeClass{
static{
//initialize stuff
}
}

Wong,
answer 'D' is correct because a STATIC REFERENCE cannot be Made through a non static method or code block. Consider this class:
class staticCheck{
static int i = 1;//(Line 1)Legal
public void initializeOther(){
static int j = 1;//(Line 2)Illegal!!! will not compile
}

public void useStaticVar(){
static int k = i;//(Line 3)Legal
}
}
Static variables and methods are created when the class is loaded before any instances have been constructed. Instance methods and variables belong to a particular instances of the class and aren't loaded untill an instance has been created.
(Line2) will not be allowed by the compiler because the non static instance method initializeOther() isn't created untill an instance of the class is constructed. Therefore there is no way of accessing the static variable when the class is loaded.
(Line3) is legal because static variables and references can be used in non-static methods but they can't be "MADE" or declared in non-static methods or code blocks.
Try compiling the code. The same idea applies to instance initializers(non-static code blocks) which take the same form as the static initializer in my InitilizeClass class except the 'static' keyword is removed.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the correct answers are B and C.
A is wrong because an instance vble is a non static one
B is correct
C is correct
I think D is wrong. I think that a nonstatic reference cannot be made through a static method or code block ( the other way around)
I am not sure about E. I think it is wrong because abstract methods are made to be overridden, and static methods cannot be overridden.
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL
Ronnie, you just stated :
" (Line3) is legal because static variables and references can be used in non-static methods but they can't be "MADE" or declared in non-static methods or code blocks. "
YOUR code snippet :
public void useStaticVar(){
static int k = i;//(Line 3)Legal
}
But in " line 3 " are you not declaring and initialising a static variable ??? Isn't this going against what you just stated ???
Am I misunderstanding something here ??
Please clarify.
Thanks in advance.
 
Ronnie Phelps
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Youre right Zahid. My bad. That was a careless mistake of mine (line 3) should read:
int k = i;//(Line 3)Legal
i is a static variable being used in a non-static method but it's not declared inside of the method. Therefore it's legal Hope this clears things up.
 
Wong KK
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I see. Thx for Everybody. Let me summerize as follows:
Question 54 from JvalTest:
Which of the following 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.
A is wrong because static can't be applied to classes
B is correct because you can only hide but not override a static method
C is correct because there's nested class
D is correct because of the word 'MADE'
E is wrong because abstract and static cannot be used together.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just hit the bulls eye!
------------------
Hope this helps. Correct me if I am wrong.
Cheers ,
Kapil
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL
What do you mean when you say you can HIDE a static method ??

Thanks in advance.
 
Your mother was a hamster and your father was a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic