0
public class constprac {

    public static void main(String args[]  )
    {
        consttest class1=new consttest("ria");
        class1.showName();
    }
}


**public** class consttest{
    String gname;
    public consttest(String name){``
        gname=name;
    }
    public String setName(){
        return gname;
    }
    public void showName(){
        System.out.println("YOUR 1ST GirlFriend IS "+ setName());
    }
}
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • Hello and welcome to StackOverflow. Please take some time to read the [help page](http://stackoverflow.com/help), especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the [Stack Overflow question checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – galath Aug 09 '15 at 08:55
  • This is how Java designed it. See http://stackoverflow.com/questions/1841847/can-i-compile-a-java-file-with-a-different-name-than-the-class http://stackoverflow.com/questions/10442758/why-must-a-java-file-have-the-same-name-as-its-public-class – M Sach Aug 09 '15 at 09:01
  • This is how Java designed it. See below links http://stackoverflow.com/questions/10442758/why-must-a-java-file-have-the-same-name-as-its-public-class http://stackoverflow.com/questions/1841847/can-i-compile-a-java-file-with-a-different-name-than-the-class – M Sach Aug 09 '15 at 09:02

1 Answers1

2

why if my file name and public class name differs then i get compile error?

Because that's how Java is defined: If you have a public class, it must be in a file named after the public class.

The details can be found in §7 of the JLS. It's essentially saying that if source code is stored in a file system, a certain set of requirements are imposed in order to make the package system work in a consistent way.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • User know that rule. He is asking for the reason behind it. `that's how Java is defined` is not an answer. – Suresh Atta Aug 09 '15 at 08:55
  • 3
    @sᴜʀᴇsʜᴀᴛᴛᴀ: I don't see any evidence of that. – T.J. Crowder Aug 09 '15 at 08:55
  • 1
    what you said is correct in case of main class. But why compile error occurs if i change the class name in other sub class with access specifier named pubilc? – Akash Prabhakar Aug 09 '15 at 08:58
  • @AkashPrabhakar: I'm sorry, I don't know what you mean by "its correct in main class". – T.J. Crowder Aug 09 '15 at 08:59
  • @T.J.Crowder if i ignore public or delete public in **public** class consttest{ String gname; public consttest(String name){`` gname=name; } public String setName(){ return gname; } public void showName(){ System.out.println("YOUR 1ST GirlFriend IS "+ setName()); } } my program works completely fine why so? – Akash Prabhakar Aug 09 '15 at 09:03
  • @AkashPrabhakar Stop beating over the bush. You are making nosense. The link (http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html) might help you to understand. Go read and ask further doubts if you have any. – Suresh Atta Aug 09 '15 at 09:05
  • 1
    @AkashPrabhakar: You've started a sentence ("if i ignore public or delete public") and never finished it. If you do that, **what**? You mean it compiles? Right, because as I said above, the restriction relates to **public** classes. – T.J. Crowder Aug 09 '15 at 09:05
  • @sᴜʀᴇsʜᴀᴛᴛᴀ 'Because that's the way Java is defined' is the only answer anybody's going to get, unless you can get Jim Gosling or God in on the call. – user207421 Aug 09 '15 at 09:41
  • @sᴜʀᴇsʜᴀᴛᴛᴀ thank you so much for your response i am the beginner for java programming as we as stack over flow – Akash Prabhakar Aug 09 '15 at 14:48
  • @T.J.Crowder THANK YOU so much for your response as i said i am the beginner for java programming . – Akash Prabhakar Aug 09 '15 at 14:49