aspose file tools*
The moose likes Beginning Java and the fly likes two public classes in same source file? 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 » Java » Beginning Java
Bookmark "two public classes in same source file?" Watch "two public classes in same source file?" New topic
Author

two public classes in same source file?

Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
Today I have given a code


public class A
{
class B b;
}

public class B
{
class A a;
}


and he asked is there any error??

I told him we can't place two public classes in a single source file..He said no it is possible.

then he changes the code as

public class A
{
class B b; // line no1
}

protected class B
{
class A a; // line no2
}

then I said line no1 n line no2 has error.
He was not satisfied, I said this forward declaration is allowed in c++ not in java..

Then at the end of the interview I again said him that its not possible...
he said it is possible either he was joking or I am mad or may be I don't know much....
Please suggest some thing...

Thanks

Living is easy with your eyes closed.....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

They all have errors; you can't just put "class" keywords in random locations.

Other than that--what do you think? What happened when you tried it?

(The idea of "forward declarations" isn't necessary in Java; classes can be defined in any order.)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

David Newton wrote:They all have errors; you can't just put "class" keywords in random locations.


Agreed. In order to have a viable debate on a particular of the language, you actually need valid language constructs.

Otherwise, the debate is simple -- Syntax error. Won't compile. Period.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
thanks...
But I don't know what he was trying to do with me...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

We don't either--if that was the exact code you were given, and it's supposed to be Java, it's just plain wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: two public classes in same source file?