File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ststic variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ststic variables" Watch "ststic variables" New topic
Author

ststic variables

Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
Please suggest me the answers of the following two programs.
Explain the reasons if u can. These are from a mock exam that do not provide with answers.

A.Compilation Error
B.Runtime Error
C.Output as x value is:0
D.Output as inside B x value is:0 ;
------------------------------------

A.Compilation Error cannot override static methods
B.Runtime Error class cast Exception
C.Output as x Value in Base:5;
D.Output as x Value in Sub:5;

[This message has been edited by Marilyn deQueiroz (edited October 16, 2001).]
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Hi Nisheet, the first one is a little bit easier. Simply D as the constructor of B is invoked first and then the println().
The second one is a very tricky question trying to divert u with the concept of polymorphism which states that methods r invoked according to the nature of the runtime object, not the refernce.So one could find according to plymorphic behavior of n object the answer should be D but the answer is C..
Because polymorphic behavior r shown by objects and only non-static methods r assoicated with objects while static methods r associated with classes. So in ur question the right method chosen by the compiler is at compile time according to the class of the object reference/variable, not the object itself.
Hope it helps....
------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
This suggests that there is no TRUE overriding of static methods between classes and subclasses. Is this true?

Darryl Failla
Sun Certified Java 2 Programmer
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Q1: D
Simple constructor invocation and static memeber printout
Q2: C
Static methods cant be overriden. So even though
polymorphic reference or lookup takes place
ie Base b = new Sub() it doesnt matter coz its static .
so aMethod() goes to reference (static binding)

Ragu
Alan Mar
Greenhorn

Joined: Oct 15, 2001
Posts: 21
I didn't get a chance to run the code in Q2. But I remembered
that static methods don't participate in overriding at all. So is it legal to have the same method signature in the sub class?
I prefer A choice in Q2. Please correct me if I'm wrong.


----SCJP Java 2----
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Originally posted by Alan Mar:
I didn't get a chance to run the code in Q2. But I remembered
that static methods don't participate in overriding at all. So is it legal to have the same method signature in the sub class?
I prefer A choice in Q2. Please correct me if I'm wrong.

Hi Alan,
The code will comiple w/o any error as it is an example of hiding. No overriding here. The correct answer is C.
Here is from JLS 8.4.6.2 Hiding (by Class Methods)
"If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method. "
HTH,
- Manish
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by darryl failla:
This suggests that there is no TRUE overriding of static methods between classes and subclasses. Is this true?

Hi Darryl -
There's no overriding of static methods, period. The confusion that arises sometimes is when a static method, in terms of its scope protection, is equated with a member that is private or final.
When a method is declared static in a class, the subclass is not prohibited from writing a method by the same name. This is a subtle distinction, I think, because it's not easy to understand the difference between method overriding and method "re-writing." The latter of course is not an intended design feature; it's just a consequence of the distinction between a member that cannot be overridden because of scope or finality, and something that gives the impression of being modifiable in a subclass.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Q1) A.Compilation Error
if you have a public static main(String arg[]) in class A
u probably want to name this file A.java right? So
it will run the main method right?
but your also have a public class B in the file.
You can't do that.
if you have B.java as your file name.
There is no output. Cuz there is no main method to be run.
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by FEI NG:
Q1) A.Compilation Error
if you have a public static main(String arg[]) in class A
u probably want to name this file A.java right? So
it will run the main method right?
but your also have a public class B in the file.
You can't do that.
if you have B.java as your file name.
There is no output. Cuz there is no main method to be run.

Actually you have a good point
But the answer is not correct
It will certainly compile and run with D output
public class deserves the file name and here it is B.java
Ok , now we dont have a main() in class B... but main() is in class A. Since A is B's parent, B inherits it and executes without problem
Hope it helps you
Ragu
Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
Hi FEI NG:
If you have a public class then the compilation unit must be saved as the name of the public class.java. So, in Q1, the file is B.java.
It will compile successfully and will give an output because, the main method from class A is inherited down to class B.
Therefore, you can compile : javac B.java
and you can run: java B
your output will be:
inside B
x value is: 0
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Static methods belong to class and thus can not inherit and main() is a static method then how come its is inherited in the subcless and executed.
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Muhammad,
The 'class' is really a template for making additional objects (instances) of the same type. Static items are available to all instances of the type but each instance does not have one as part of it.
Imagine the way appliances obtain electricty in your home. There are thousands of 'houses' but all of them are serviced by one generator. A generator is not built in every house.
The same applies to static variables or methods. If a subclass does not declare a main() method of it's own, it still has access to the main() method declared for it's type or class.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited October 16, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Yin Ming
Ranch Hand

Joined: Oct 12, 2001
Posts: 41
Q2:c
I meet this before, and someone told me that here is no polymorphic. these code
Base b=new Sub();
b.aMethod(5);
is just equals to Base.aMethod(5). so the answer is C.
becoz they are static member.
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Thanks Jane, I understand now that the static members are not inherited but are still accessible.
--Farooq
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
confusion....
if static methods are not inherited then how come they can be accessed without using class name ???
------------------
Regards
Ravish


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Originally posted by ravish kumar:
confusion....
if static methods are not inherited then how come they can be accessed without using class name ???

Static methods are inherited, they *can not* be overriden.
- Manish
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Muhammad Farooq:
Thanks Jane, I understand now that the static members are not inherited but are still accessible.
--Farooq

Farooq Please read Manish quote... U made me confuse
Thanks Manish
------------------
Regards
Ravish
[This message has been edited by ravish kumar (edited October 17, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ststic variables