• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Public class

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is from MARCUS tutorial

*********************************************
A file can only contain one outer public class. If you attempt to create a file with more than one public class the compiler will complain with a specific error. A file can contain multiple non public classes, but bear in mind that this will produce separate .class output files for each class
**********************************************

It's fine that a file should have only one "outer public" class.

What will happen if we have many "inner public" classes will they be considered as simply a class and compiled as a separate .class file.

For example

public class A
{
public static void main(String nat[])
{

System.out.println("hi XXXXX");
}


public class B
{

System.out.println("hi YYYY");//works fine without this statement
//but generates error when this statement is included.
}
}

Can any of you please explain what's the problem when we include the statement ?

Thanks
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error is not occurring because of two public classes. As Marcus states, you can only have one outer public class. You can have as many public inner classes as you'd like. Each class, public or not, will be compiled into its own class file, even inner classes.

The error you're getting is due to the fact that your println statement is floating in "no man's land." It isn't in a valid block. If you had put that line into a valid block, such as a method body, a constructor, or an initializer, I believe that your error would go away.
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !

Also there will be two classes are formed :

1) A.class
2) A$B.class
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic