Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

two public classes in same source file?  RSS feed

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
author
Sheriff
Posts: 23586
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks...
But I don't know what he was trying to do with me...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!