File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question

 
chetna_jain
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Outer {
private Outer() {
System.out.println("Executing Step1");
}
public void Outer() {
System.out.println("Executing Step2");
}
}
public class q2 extends Outer {
public static void main (String[] args) {
Outer t = new Outer();
t.Outer();
}
}

what is the output of this code and why is it so.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code does not compile since the default constructor of Outer is private and thus not usable outside the Outer class.
If you change the modifier private to public for the default constructor the output is
Executing Step1
Executing Step2
which makes sense, the first line is written when the constructor is called and the second when you call the method Outer !
But this is poor design to name a method the same name as the class name since it can be misleading and error-prone.

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code doesn't compile because the constructor is private.
Yes it seems that the compiler doesn't complain having a method with the same name as the Class or even an identifier for a field:
int Outer; //I have checked it out
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic