This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic