File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java public class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "java public class" Watch "java public class" New topic
Author

java public class

Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 30

In java if we have a public class then what is the need to save the file with the same class name.
example...
Public class dog{

code here...
}

then we need to save the file as dog.java.

Why?
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 318
This has been discussed many times here. Try searching. The best answer you will probably come up with is that that's what the designers of java decided to do. If you want to know the thinking behind that decision you will probably have to ask them.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

first of all it is public , not Public and it is a convention to start class name with capital letter.

and now test below scenario.

class Test{} save file as Cool.java and try javac Cool.java ---> and what you find?
(and)
public class Test{} save file as Cool.java and try javac Cool.java ---> and what you find?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and Welcome to JavaRanch Aneek Banerjee
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Seetharaman Venkatasamy wrote:first of all it is public , not Public and it is a convention to start class name with capital letter.

and now test below scenario.

class Test{} save file as Cool.java and try javac Cool.java ---> and what you find?

I think, it will compile, as there is no public class so any name can be used.

Seetharaman Venkatasamy wrote:
public class Test{} save file as Cool.java and try javac Cool.java ---> and what you find?


It'll not compile as there is a public class exists.

Tell the difficulties that i am difficult.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 867

saloni jhanwar wrote:
Seetharaman Venkatasamy wrote:first of all it is public , not Public and it is a convention to start class name with capital letter.

and now test below scenario.

class Test{} save file as Cool.java and try javac Cool.java ---> and what you find?

I think, it will compile, as there is no public class so any name can be used.

Seetharaman Venkatasamy wrote:
public class Test{} save file as Cool.java and try javac Cool.java ---> and what you find?


It'll not compile as there is a public class exists.


you are right. but Seetharaman intended that question for Aneek Bannerjee. You are now experienced Saloni , i think we should give chance to novice to answer the question.


OCPJP 6(100 %) OCEWCD 6(91 %)
Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 30

Seetharaman Venkatasamy wrote:and Welcome to JavaRanch Aneek Banerjee


Thanks,,,and ya its giving the compilation error.I checked...Thanks,,,

And sorry for the"Public"...:P.....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Aneek Banerjee wrote:Thanks

You are welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java public class
 
Similar Threads
why to save same name as class
why do we need to save the java file with the name of public class file
i am in cofusion
Compiling in eclipse
dan's sigle topic--arrays