• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ststic variables

 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This suggests that there is no TRUE overriding of static methods between classes and subclasses. Is this true?
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Yin Ming
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jane, I understand now that the static members are not inherited but are still accessible.
--Farooq
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
confusion....
if static methods are not inherited then how come they can be accessed without using class name ???
------------------
Regards
Ravish
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic