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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question" Watch "Question" New topic



Joined: Aug 25, 2001
Posts: 28
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();

what is the output of this code and why is it so.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
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

[Blog] [Blogroll] [My Reviews] My Linked In
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: Question
It's not a secret anymore!