3. Quiz Question: Why only 1 Public Class in JAVA file

Поделиться
HTML-код
  • Опубликовано: 19 сен 2023
  • Join this channel to get access to perks:
    / @conceptandcoding

Комментарии • 46

  • @eshwarsai5027
    @eshwarsai5027 2 месяца назад +2

    one of the finest explanations. Great!

  • @DeepakKumar-yl3ok
    @DeepakKumar-yl3ok 3 месяца назад +4

    But if you are saying that when the filename is equal to the class name, JVM does not need to know about what all classes are present inside the file, it can directly call the class which has the same name as file name, then why is there a need of only 1 public class
    file -> B.java
    two classes -> public class A {}
    public class B {},
    so even though there are two public classes, JVM knows that it would call B java, because that is having the same name as file name

    • @ritikagrawal9768
      @ritikagrawal9768 Месяц назад +1

      yeah same doubt

    • @vedanshgaur9517
      @vedanshgaur9517 Месяц назад +1

      Generally other classes should be kept private or protected withing the package for security reasons. Surely you can make every class public but it is not a good practice.

  • @weirdstrategist
    @weirdstrategist 9 месяцев назад +7

    hi, i have a main.java and as part of that i have the following code
    public class Main {
    // This class is empty and does not have a main method.
    }
    class Helper {
    public static void main(String[] args) {
    System.out.println("Hello, World!");
    }
    }
    this compiles and creates two classes Main and Helper and if i run helper then i can see the hello world , so seems like main() need not be linked to the public class always.

    • @ulavaashok7381
      @ulavaashok7381 12 дней назад +1

      If we are in same package then we can access the class within same package and it will execute the main method, But if class is not public then it won't be accessed outside the package, see below classes.
      Note: As per java convention rules and JVM it's best practice to keep main method in public class
      package package1;
      class Hello{
      public static void main(String args[]){
      System.out.println("Hello World");
      }
      }
      package package2;
      import package1.Hello;
      public class Main{
      //Empty class as per you
      }
      class Hi{
      public static void main(String[] args) {
      //calling main method from Hello class, where it will throw error
      //since Hello class is not public
      String[] arr = {"1", "2"};
      Hello.main(arr); // Calling the main method of Hello class
      }
      }
      Where it will throw below compilation error
      Main.java:3: error: Hello is not public in package1; cannot be accessed from outside package
      import package1.Hello;
      ^
      Main.java:14: error: Hello is not public in package1; cannot be accessed from outside package
      Hello.main(arr); // Calling the main method of Hello class

  • @CodingJoySoul
    @CodingJoySoul 10 месяцев назад +2

    Please make videos more frequently.

  • @user-hn1gs3nl7z
    @user-hn1gs3nl7z Месяц назад +1

    We can create a class with employee and can keep its name to other rather than employee which is same name for class and guess what it still runs but when it runs then jvm create a .class file name with the class name . i.e employee

  • @shwetanksudhanshu4383
    @shwetanksudhanshu4383 8 дней назад

    Hello, can you please make the other videos are public instead of members only :) , it would really help. Thanks

  • @ShubhamRajput23
    @ShubhamRajput23 7 месяцев назад +1

    Hi Shreyansh,
    While I can understand that only 1 public class is allowed per file but I defined 2 class in my file both with access modifier as default(package-private) and one of them contained main method. There was no error in the program and it ran successfully. So does that mean JVM has capability to search for main method in multiple default classes if there is no public class defined?

    • @ConceptandCoding
      @ConceptandCoding  7 месяцев назад +2

      Generally, In Java, JVM looks for the "public static void main(String[] args)" method to serve as the entry point for a standalone Java application. The main method must be declared within a class, and that class needs to be specified when running the Java program.
      If you have multiple classes in the same file, each with its own main method, But that file name should match with one of the class right, so while running the code, most probably it will pick that class which name is equivalent to file name.

    • @mils3318
      @mils3318 4 месяца назад +2

      @@ConceptandCoding I just tested this claim of yours using Java21 in InteliJ.
      I created a .java file with a certain name and in it defined two default classes with completely different names and without defining a public class (of course the same name as the name source file).
      In both of those classes, inside that source file, I defined classic main methods that print different messages in the console.
      When I started the program, it called and printed in the console the contents of the FIRST main method it encountered defined in that file.
      So, to define the main method, it is not necessary to write it in the public class, it can also be written in the default class. And the program is carried out regularly. Probably the JVM looks ONLY at the exactly defined signature of the main method according to the Java language specification.
      But it is worth noting that it is BEST practice to define public class and inside main method.

  • @ANSHULGUPTA880
    @ANSHULGUPTA880 Год назад +2

    Still this doesn't clarify why JAVA files with NO main methods are also restricted by same design?

    • @ConceptandCoding
      @ConceptandCoding  Год назад +3

      Couple of reasons only I can think of:
      - doing this restriction, efficiency of compiler might have improved, as it knows public class name and File name would be same.
      - second, it bring some code structure and readability

  • @vishwasdhanwani6134
    @vishwasdhanwani6134 10 месяцев назад +2

    Hey Shrayansh, I don't think main method must be inside public class as jvm will read .class or bytecode file and jvm will be able to read the main methods.
    To be extra sure my knowledge, I tried with an example
    class MainClass11 {
    public static void main(String[] args) {
    System.out.println("hello vishwas");
    }
    }
    public class NotMainClass11 {
    }
    This compiles & creates 2 classes. And if you run >> java MainClass11 then it will run main method().
    I hope, this was helpful.
    Thanks
    BTW love your content😁😁

    • @ConceptandCoding
      @ConceptandCoding  10 месяцев назад +1

      Thanks buddy.
      For this example you have created 2 different files MainClass11 and NotMainClass11.
      Could you pls try putting both the class in the same file and then try.

    • @dhirajrana397
      @dhirajrana397 7 месяцев назад

      @@ConceptandCoding I put them in same class, but still it runs:
      public class PublicClass {
      public int p;
      }
      class NonPublicClass {
      public static void main(String[] args) {
      PublicClass publicClass = new PublicClass();
      System.out.println(publicClass.p);
      }
      }

    • @harshitanand7349
      @harshitanand7349 7 месяцев назад

      Agreed with Vishwas. I can define main method in non-public class as well.
      Let’s say I have a java file Demo.java. Your ide will automatically write public class Demo{}
      Create a non-public class say Hello inside this Demo file. You can add main method inside this Hello class and it will compile without any error.
      Yes one thing is there. If you compile the file it will create Demo.class file and when you’ll run it the JVM will call the public class main method and not the non-public class main method.

    • @rwordspecialist6734
      @rwordspecialist6734 27 дней назад

      @@dhirajrana397 bro but if we don't define if it's public or private class then it's considered as public class therefore your second class named 'NONPUBLIC ' is also a public class.
      So the jvm searches for the public class with main function in it correct me if I'm wrong.

  • @mils3318
    @mils3318 4 месяца назад +1

    I would correct you a lil bit for the 2nd point, PUBLIC class name MUST be same as file name (source file), otherwise it will be compilation error.

  • @sushantbasak1647
    @sushantbasak1647 5 месяцев назад +1

    Hey Shrayansh, Suppose I have a file name as HelloWorld.java. Inside it I created the HelloWorld1 class.
    I am then using the javac to convert it into HelloWorld1.class.
    After that I can simply call java HelloWorld1 and it is working.
    I am confused as you mentioned that Class name should match with the FileName. Can you please help?

    • @sushantbasak1647
      @sushantbasak1647 5 месяцев назад +1

      Ref code -
      import java.io.*;
      class HelloWorld1 {
      public static void main(String[] args) {
      System.out.println("HelloWorld");
      }
      }
      javac HelloWorld.java
      java HelloWorld1

    • @Study-lo6vh
      @Study-lo6vh 5 месяцев назад +1

      @@sushantbasak1647 Your public class name should match the file's name.

  • @crackit3974
    @crackit3974 10 месяцев назад +2

    Hi bro, I need your advice,
    I am a recent graduate of IIIT. I worked as an 6 month SDE Intern at Amazon .
    I received an inclined vote but due to headcount, did not receive offer,
    I have been actively applying to companies, I applied around 150+ companies, I received only 2 interview calls, I got one offer , startup said that they will give me 3 month internship(20k)+ PPO ( base 6L) (location: Jaipur)(role: Java Backend Developer)
    I am not understanding what to do, I am from Telangana

  • @shailjasharma302
    @shailjasharma302 10 месяцев назад +1

    Hi, I had a question. If there is only one public class in the program, JVM will know where to find the main method. Why does the name need to be same as that of the class as well?

    • @ConceptandCoding
      @ConceptandCoding  10 месяцев назад +1

      There can be 100 of public classes but in one file there can be only 1 public class.
      And that class name should be same as of file name.
      Otherwise if in a single file there are 3 classes how JVM knows which class has the main method

    • @rohithchittibommala2002
      @rohithchittibommala2002 10 месяцев назад +2

      ​@@ConceptandCodingbut java doesn't allow multiple public classes in a single file

    • @ConceptandCoding
      @ConceptandCoding  10 месяцев назад +1

      right@@rohithchittibommala2002

    • @akashparihar515
      @akashparihar515 5 месяцев назад +1

      Shreyansh, i have the same doubt, if we have the only one public class in a file then why the class name should be the same as file?
      As there is only one public class in file jvm can easily identify where to call the main method?

  • @napster5944
    @napster5944 Год назад +1

    I am not able to download all videos from this playlist, i like to have it on my local

    • @ConceptandCoding
      @ConceptandCoding  Год назад +2

      There might be some plugin available, check once.

    • @ANSHULGUPTA880
      @ANSHULGUPTA880 Год назад +1

      bcos its member only...google restrictions

  • @BJPTamilNaidu
    @BJPTamilNaidu 2 месяца назад +1

    have you ever used main method in real projects?

    • @ConceptandCoding
      @ConceptandCoding  2 месяца назад

      yes daily,
      whenever we start the server, spring boot internally invoke SpringApplication class, and main method act as a entry point for that

    • @BJPTamilNaidu
      @BJPTamilNaidu 2 месяца назад +2

      @@ConceptandCoding before spring boot introduced ?

    • @ramdafale
      @ramdafale Месяц назад

      @@BJPTamilNaidu Yes. It's in all java project.

  • @pleasantdayNwisdom
    @pleasantdayNwisdom 7 месяцев назад +1

    Video 4

  • @chetanm898
    @chetanm898 7 месяцев назад +1

    How to become a member ?

    • @ConceptandCoding
      @ConceptandCoding  7 месяцев назад +1

      You will find join button at channel home page Chetan.

    • @chetanm898
      @chetanm898 7 месяцев назад +1

      @@ConceptandCoding I am not able to see join button next to subscribe I am trying it from mobile

    • @chetanm898
      @chetanm898 7 месяцев назад +1

      Please reply

    • @VibrantAmit
      @VibrantAmit 6 месяцев назад +1

      @@ConceptandCodingI’m also unable to find

    • @user-br9nz1ge6d
      @user-br9nz1ge6d 6 месяцев назад +1

      There is no join button there