• 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

Source File Declaration Rules Collision

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic