This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!

deepujain Jain

+ Follow
since Mar 23, 2005
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by deepujain Jain


Class B
//throws an exception

Class A
B memX = B.someMethod();

A() {
//This constructor is not called


Class D {
someMeth() {
Class A = new A();

As u see the memX object of type B is intialized by calling static method someMethod(). This method throws an exception,
My question is if it throws an exception will this result in my constructor for class A not being invoked.
How can I fix this issue. Should I catch the exception at the time of instantiating an object of Class A.
15 years ago
If the base class method say meth() is private and you define a new method that has the same signature as meth() that is in base class. you have defined a new method in sub-class . Since the names are sane i guess this wat is known as redeclaration.
However the compiler will flag an error if any attempt is made to redeclare a method that is not overloaded.
The statement A.B() is in the outer class instance method.When this method is invoked it will already have an outer class instance.So its invoked on an outer class instance.
When you replace it with B().
Since this references to the currently executing object,and as here it is a outer class instance so basically both refer to same.

And here you are basically resolving the name, I mean you are giving the full name of class B
There are 3 ways of identifying B
just B

A super class reference can hold any of the sub-class objects without any type casts, When you do so you can access only the methods that are defined in the super class, Any attempt to access the sub-class method will result in compilation errot, However if you overrided a method in sub-class and when you call the method (Essentially both have the same name) It will run and finally the method in the child class is called.{Late binding}.

However when you want to assign a super-class reference to a child class you will have to explicitly type caste it to your child class, Or else the compiler will complain.

P p = new C1();
C2 c = (C2)p;
If the super class refernce is refencing to some other child class (C1) then you will get a class caste exception, But the complier wont complain.
You can avoid this class caste exception if you use isntanceof
if(p instanceof c)
C2 c = (C2)p;
So now this condition will fail and you wont get any exception,