• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

public class

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the following code.

package pack11;

public class Test {

public void method() {

System.out.println("In method()");
}
}

public class Test11 {

public static void main(String[] args) {

new Test().method();
}
}

Compilation Error: class Test must be declared in the seperate file since it is public. WHY???

Could any one explain this conceptually?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You Are Creating An Object Of The Class But Not Asssigned To any Refrence.

Alternate solution If u Use System.out.println It Will work fine.

i.e System.out.println(new Test().method());
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B'coz we have only one public class.JVM doesn't allow two public classes.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There can not be 2 public class definations in one source file. If you create 2 fifferent files as Test.java and Test11.java with proper package declaration, it will work.

No issues with JVM as we are not yet able to compile the class.

No need to put system.out.println


Rajesh Godbole
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will tell you the concept behind this.

You can specify only 1 public class per 1 source file because JVM will search the main() definition in this class.

Suppose you were allowed to write 2 public class in one java source file, then how will JVM knows which main() method to invoke from these two classes.

Moreover your public class name should be same as java source file name...

Let me know if you sill have any doubt.....
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic