• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

two calss in one singel .java file?

 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if there are two class in a .java file ,one class is declared public why the other can't
declared protected or private (the compiler complain that :there is no declaration)
 
Nitin Mishra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can use modiefiers with inner classes only
 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give the clearly concept of a modifier,and whethe it includes access modifier(public private,protected,default)?
 
Snylt Master
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Nitin
Access modifers are private , protected , public and default.
You don't type default only class test{}.
A class can only be marked as public or default. Methods can be marked with private , protected , public and default. Inner classes can be marked private , public , default. I don't know if an innerclass can be marked protected. By i think you can. Anyone who can fill me in.
// Snylt Master
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3714
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>if there are two class in a .java file ,one class is declared public why the other can't
Well, as Java is sometimes called C++ --, this is one of the rules meant to increase maintainability, productivity and reduce confusion, error prone constructs etc.
Suppose you have two public classes in one java file. Now you quit the job and someone else tries to run one of the public classes whose name does not match the name of the java file and finds that there is a bug. He/She will surely have a trouble finding what file this class is defined in.
Yes, this can also happen with non-public classes defined in one file and although it is not prohibited but it is definitly discouraged unless all the classes in that file are strongly related.
HTH,
Paul.

------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic