Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Source File Declaration Rules Collision Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Source File Declaration Rules Collision" Watch "Source File Declaration Rules Collision" New topic
Author

Source File Declaration Rules Collision

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Rule>> Files with no public classes can have a name that does not match any of the classes in the file.

Then how my program is working with same name for file,that is already used for non public class name ??

Thank You in advance.


Source File Name->>Moo.java


OUTPUT
---------- java ----------
publicMethod()
protectedMethod()
defaultMethod()

Output completed (0 sec consumed) - Normal Termination


Tell the difficulties that i am difficult.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

saloni jhanwar wrote:Rule>> Files with no public classes can have a name that does not match any of the classes in the file.

Well, it doesn't mean that if a .java file does not contain a public class, then its name must be something altogether different? The rule only says that
1) One .java file can contain maximum one public class.
2) If a file contains a public class, then name of the file must be same as that of public class.
So, if a file doesn't contain any public class, its name can be any valid file name.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
So, if a file doesn't contain any public class, its name can be any valid file name.

When I tried different name other than non public classes, then my program doesn't run with that, then what is use to put different name and why should i use.Thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

saloni jhanwar wrote:
Anayonkar Shivalkar wrote:
So, if a file doesn't contain any public class, its name can be any valid file name.

When I tried different name other than non public classes, then my program doesn't run with that, then what is use to put different name and why should i use.Thanks

What do you mean by 'program doesn't run'? Have you written your main method in non-public class? If yes, then it won't work.
Secondly, its just a facility from Java. The language only restricts file name if the file contains one public class. However, in reality, it is always better to create different files for different classes.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:


Thank You I got that if i want to run program then i have to use the file name same as class name which has main method but if i dont want to run program then i can use any name for file it will compile fine but wont run.
Norbert Muench
Greenhorn

Joined: Mar 09, 2012
Posts: 19
Anayonkar Shivalkar wrote:What do you mean by 'program doesn't run'? Have you written your main method in non-public class? If yes, then it won't work.

Not true. The following will compile and run fine, although the main method is not in a public class:

saloni jhanwar wrote:When I tried different name other than non public classes, then my program doesn't run with that, then what is use to put different name and why should i use.Thanks

What do you mean by that? If I take your example code above and save it in a file called foo.java, the code will compile and run without problems.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Norbert Muench wrote:Not true. The following will compile and run fine, although the main method is not in a public class:

Yes. You are right.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
Norbert Muench wrote:Not true. The following will compile and run fine, although the main method is not in a public class:

Yes. You are right.


Hi Anayonkar Shivalkar
You were giving me wrong knowledge that is not good .Every greenhorn posts doubts here to get clarification not to get more confused.This all will lead to frustrating about this forum.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Norbert Muench wrote:
saloni jhanwar wrote:When I tried different name other than non public classes, then my program doesn't run with that, then what is use to put different name and why should i use.Thanks

What do you mean by that? If I take your example code above and save it in a file called foo.java, the code will compile and run without problems.



Hi Norbert,thanks for first clarification.
In second,you didn't get my point, yes above code will work fine ,because you are compiling file using source file name while interpreting using class name that is different scenario but i asked that if i interpret also using same source file name ? then it wont work.
Norbert Muench
Greenhorn

Joined: Mar 09, 2012
Posts: 19
saloni jhanwar wrote:
Hi Norbert
You didn't get my point, yes above code will work fine ,because you are compiling file using source file name while interpreting using class name that is different scenario but i asked that if i interpret also using same source file name ? then it wont work.

Of course it won't work. The name you pass in as a parameter to the java vm is the name of the class with the main method in it, not the source file. If I would have tried to run "java foo" in my example above, the java vm would have looked for a class named foo (which in turn means the classloader would have looked for a file named foo.class anlong the class path). As such a class does not exist, you get an error.
saloni jhanwar wrote:Hi Anayonkar Shivalkar
You were giving me wrong knowledge that is not good .Every greenhorn posts doubts here to get clarification not to get more confused.This all will lead to frustrating about this forum.

Now come on, this hardly seems fair. Anayonkar just tried to help you and made an honest mistake. That can happen to everyone. I think you should apologise to him.
The lesson to take away here is that you should always verify the answers you get, instead of blindly accepting them. In this concrete example you could have noticed, that Anayonkars answer actually contradicts your findings in your first post in this thread.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Norbert Muench wrote:
Now come on, this hardly seems fair. Anayonkar just tried to help you and made an honest mistake. That can happen to everyone. I think you should apologise to him.
The lesson to take away here is that you should always verify the answers you get, instead of blindly accepting them. In this concrete example you could have noticed, that Anayonkars answer actually contradicts your findings in your first post in this thread.


I'm beginner and as any beginner if I've confusion in some topic and someone explain that this will be correct then I've to accept it regardless that is actually right or wrong because i've been already confused. Anyways i won't apologies because i'm not wrong.thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

saloni jhanwar wrote:You were giving me wrong knowledge that is not good

My apologies. It was not intentional, and I should've confirmed it before posting in forum.

However,
Anayonkar Shivalkar wrote: So, if a file doesn't contain any public class, its name can be any valid file name.
I guess I am correct with this statement.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
saloni jhanwar wrote:You were giving me wrong knowledge that is not good

My apologies. It was not intentional, and I should've confirmed it before posting in forum.



It's ok.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

saloni jhanwar wrote:
Norbert Muench wrote:
Now come on, this hardly seems fair. Anayonkar just tried to help you and made an honest mistake. That can happen to everyone. I think you should apologise to him.
The lesson to take away here is that you should always verify the answers you get, instead of blindly accepting them. In this concrete example you could have noticed, that Anayonkars answer actually contradicts your findings in your first post in this thread.


I'm beginner and as any beginner if I've confusion in some topic and someone explain that this will be correct then I've to accept it regardless that is actually right or wrong because i've been already confused. Anyways i won't apologies because i'm not wrong.thanks


Yes, you were wrong. Anayonkar Shivalkar is a knowledgeable and helpful individual. He made a simple, honest mistake, just like you do and I do and everybody does every single day. There was no call to attack him like you did. And his mistake was quickly pointed out and corrected. That's one of the great things about a public forum like this, is that even if somebody posts something incorrect, somebody else will probably see it and correct it. (Just like I corrected your inaccurate post about heap and stack a few minutes ago.)

Additionally, the idea that "I've to accept it regardless that is actually right or wrong" is wrong. You need to apply your own intelligence and judgment to everything you read.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Jeff Verdegan wrote:
saloni jhanwar wrote:
Norbert Muench wrote:
Now come on, this hardly seems fair. Anayonkar just tried to help you and made an honest mistake. That can happen to everyone. I think you should apologise to him.
The lesson to take away here is that you should always verify the answers you get, instead of blindly accepting them. In this concrete example you could have noticed, that Anayonkars answer actually contradicts your findings in your first post in this thread.


I'm beginner and as any beginner if I've confusion in some topic and someone explain that this will be correct then I've to accept it regardless that is actually right or wrong because i've been already confused. Anyways i won't apologies because i'm not wrong.thanks


Yes, you were wrong. Anayonkar Shivalkar is a knowledgeable and helpful individual. He made a simple, honest mistake, just like you do and I do and everybody does every single day. There was no call to attack him like you did. And his mistake was quickly pointed out and corrected. That's one of the great things about a public forum like this, is that even if somebody posts something incorrect, somebody else will probably see it and correct it. (Just like I corrected your inaccurate post about heap and stack a few minutes ago.)

Additionally, the idea that "I've to accept it regardless that is actually right or wrong" is wrong. You need to apply your own intelligence and judgment to everything you read.



ok thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Jeff Verdegan wrote:Anayonkar Shivalkar is a knowledgeable and helpful individual.

Thanks Jeff Verdegan It feels nice when someone like you says that.

As the question in original post has been answered, let's cool down now
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
Jeff Verdegan wrote:Anayonkar Shivalkar is a knowledgeable and helpful individual.

Thanks Jeff Verdegan It feels nice when someone like you says that.

As the question in original post has been answered, let's cool down now


hmm I think, you had reported about this that's why he posted this all.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

saloni jhanwar wrote:hmm I think, you had reported about this that's why he posted this all.

No. I didn't report it - because there was a mistake in my post and that is why I clearly apologized about that. Frankly speaking, I wouldn't make such guess. I don't know why you feel/guess that way.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
saloni jhanwar wrote:hmm I think, you had reported about this that's why he posted this all.

No. I didn't report it - because there was a mistake in my post and that is why I clearly apologized about that. Frankly speaking, I wouldn't make such guess. I don't know why you feel/guess that way.



Anyways, all be happy, I am wrong.
 
Don't get me started about those stupid light bulbs.
 
subject: Source File Declaration Rules Collision