Tuesday, July 8, 2008

File name and class name should be same?


There is a relation between the name of java source file and the public classes you can declare in that file - only the public classes..okay..not classes with any other access level. Hey by the way do you know which access modifiers can be applied to the outer classes/non-inner classes?...that will be a fantastic post in itself...coming soon.. :) So, coming back to the point, yes there is a constraint on the file name when you want to declare a class in the file as public. The constraint is that the file name should be same as that of your public class name.

But it's not that simple to understand by this single sentence, when I heard first time about this thing I had following doubts:

1. Can I have more than one public classes in my java file?

2. Should the class be public to be called by the JVM ?

3.If a class is not public but has a main method can it be called by the JVM? If yes then how the hell is it possible??? And if not ...why?? what's the problem in that??

There is no simple solution to these questions than coding for yourself and thats what I did!

Take a look at the code carefully, it's in the java source file named as example6.java

Let me tell you some things in this example. The source file name is example6.java and there is no class called as example6 in the source file. There are two classes class1 and class2 which are not public!

Doubts Answered:

1. NO! I tried to give public access to class1 and class2 and compiler scolded me for two things - one he said that class1 if public should be declared in file named class1.java, other is - there can't be any two public classes in a single java file.

2. Not al all! You can see that class1 and class2 are not public, still I am calling both of them!

3. Yes it can be called if it has a main method! You can see that class1 is not public but has a main method, so I can call it by giving parameter class1 to the java command.

I hope you have throughly understood the constraints. Just to revise all, here is the conclusion...

Conclusion: The above program proves that...

1.There is no need that the classes whose main method you are going to call should be public.

2.File name can be anything as long as you dont have any public classes in your file.

3.Which main method will execute only depends upon which classname you pass to the JVM that is as argument to java command.

4. If you want global access of a class and hence declare that class as public then it should be in the same file as the class name, and as you cant have two different names for a single file you can't have two public classes in a single .java file!

10 comments:

Kiran Shigli said...

Hi Saurabh,

Nice info. I've quick question. what might be the worst case if I don't declare any of the classes as public? Thanks, Kiran

Saurabh Patil said...

The quick answer:
No problem at all! The public access modifier has to be used where you want the class to be globally visible. If you dont want that, then you should not declare it as public. You can see that the two classes we used class1 and class2 in our example are not public...and still everything runs fine! There is nothing as best case amd worst case, if you want to make a class globally visible then make it public, if you want that class to have package level access then give it default access. Thats it!

Thanks a lot for your comment kiran, hope you keep visiting this blog and firing more questions! :)

Regards,
Saurabh

Adil Fulara said...

Great Post. Didnt know that.

Btw, good to see a fellow Sangli citizen on the net.

~Adil.
Los Angeles.

Kiran Shigli said...

Thanks for the clarification Saurabh!!

brindha said...

Hi Saurabh,

After reading your blog, I got a doubt . Can you please tell me when there are one default class and one public class present in a java file which is saved in the name of the public class.

For an example:
/* start of java file - second.java */
class first
{
public static void main(String args[])
{
System.out.println("First non public class");
}
}
public class second
{
public static void main(String args[])
{
System.out.println("Second Public class");
}
}
/* End of java file - second.java */

When you execute "java second" which class main method will be executed??

a)Whether the first main method in the file ??

or

b) The main method present in the public class.

brindha said...

Hi Saurabh,

After reading your blog, I got a doubt . Can you please tell me when there are one default class and one public class present in a java file which is saved in the name of the public class.

For an example:
/* start of java file - second.java */
class first
{
public static void main(String args[])
{
System.out.println("First non public class");
}
}
public class second
{
public static void main(String args[])
{
System.out.println("Second Public class");
}
}
/* End of java file - second.java */

When you execute "java second" which class main method will be executed??

a)Whether the first main method in the file ??

or

b) The main method present in the public class.

Saurabh Patil said...

hi brindha,

Thanks for showing interest.

Here's the answer for your question:

When you say "java second" what are you exactly doing is telling java interpreter through the argument that it should run on class second, so the main method of second class will be called by the JVM, so "Second Public class" will get printed.

Hope you understood, if not fire your doubt!

Regards,
Saurabh

Divyang Kansara said...

I have one doubt

package basicprograms;

class Example {


public static void main(String[] args) {

System.out.println("Example");
}

}

private class second {

public static void main(String[] args) {

System.out.println("Second");
}

}

When I give access specifier as private then compiler says that only public , final and abstract are allowed.. Why is it so?

Saurabh Patil said...

Hi DIvyang,
That is a terrific question!

The only access modifiers that can be applied to a top level class is public and default. You cannot apply Private and Protected access modifiers to a top level class. Why?

Private access modifier suggests that the entity is visible only inside that class. It has a meaning when applied to a member inside a class, but it doesn't have any meaning when private is applied to a class. What would a private class mean? Think about it!! It means nothing! Same goes for Protected - visible to the subclasses. A class is protected!? And a class (and not the member variables) is visible to its sub-classes!!?? It means nothing!

Now, all this is true about a top level class, an inner class can be private/protected, because that is a member inside the top level class!

This question deserves a special post. Thanks for this question!

Regards,
Saurabh

Unknown said...

Nice one..very informative