Thumbnail for Java Tutorials || Java OOPS ||Java Source File Structure Introduction || by Durga Sir by Durga Software Solutions

Java Tutorials || Java OOPS ||Java Source File Structure Introduction || by Durga Sir

Durga Software Solutions

24m 39s3,220 words~17 min read
Auto-Generated

[0:10]Sir, in the last videos we covered flow control, operators, language fundamentals, these kind of topics we discussed, even the possible practice bits also we discussed in detail. Sir, now the next topic what we have to discuss Java source file structure, very, very important sir. What is that? Can you can you spell out? Java source file structure. Okay, here we have to talk about package statement, import statement, all these things will come in the picture, very important. Slowly we are entering into next level of Java, means basic is completed, now the second module is ready to start. Sir, you required to take a bit special care about these things right. Sir, now I have one small doubt sir. What is my doubt is in Java program. Sir, we write several Java programs, in Java program, how many classes we can take? Can you can you spell out? How many, how many classes we can take? Is it only one class or can I take any number of classes? Is it only one class or can I take any number of classes? Yes, make sure in Java program, we can take any number of classes, no problem at all. Suppose I'm writing a program, sir. Yes, class A, class A. Next the class B, I'm taking. Next the class C. Okay. Class D I'm taking sir. So total A, B, C, D, like this, four, four classes are available, right? Okay. Now sir, this total is a single Java program. Remember this one, a Java program can contain any number of classes. Don't feel sir, it is applicable, it allowed only one class, no. Any number of classes we can take. Okay, well. Sir, how many classes are there? A, B, C, D, like four classes are there. Can you please tell which, which class name I want to use for this program name? Can I use A.java or can I save this program with B.java name or can I save this program with C.java name? Or can I save this program with Durga.java? Have you observed? Durga.java, Durga class is not there here. Can I save this one with Durga.java? Yes, please confirm. Based on which rule we have to give the name for this Java program? Sir, very simple for you people. Okay? Here for this example, you can use any name, no problem at all. Are you getting any name, no problem at all. Sir, what is the rule to name a Java program? Sir, very simple, a Java program can contain any number of classes, any number of classes, no problem at all. How many public classes we can take? Have you observed, how many public classes we can take? Sir, you are taking several classes and any name you can use, no problem at all. How many public classes we can take? Sir, make sure even it contain several classes, but a Java program can contain at most one public class. Are you getting what is the word, at most one? What is the meaning of at most one? Either zero or one. Sir, zero number of public classes are allowed or if you want to take one, one public class is allowed. At most one public class, by mistake, more than one public class if you declare in the same Java program, compiler will give left and right. Remember that syntactical mistake it is. Sir, how many public classes are allowed? Only one public class is allowed, more than one public class if you are trying to take, immediately compatible error by default you are going to get. I hope everyone can be able to understand right. Okay? Sir, now if I can take, if there is no public class, assume, if there is no public class. Any name you can use for Java program, any name, any name you can use for Java program, no problem at all. But, but if there is a public class, assume that class B is the public sir. Class B is the public, compulsory, the name of the Java program should be B.java only. Are you getting compulsory name of the Java program should be B.java only. By mistake, if you are using any other name, immediately compile time error we are going to get. Remember that. Sir, let me repeat again so that you people can feel more comfortable, right? Sir, here observe a Java program can contain any number of classes, but at most one class can be declared to the public. If there is no public class, no public class, if there is no public class, any name you can use for Java program, no problem at all. But, if there is a public class, if there is a public class, compulsory name of the public class, name of the program must be same. If a class be the public, the name of the program should be B.java only. Is it clear for all of you right? Sir, now let me show a small example, so that you people can aware much clearly right. Observe carefully sir, here I'm taking.

[5:51]Here I want to take a separate Java code, here observe that sir. I'm taking class A. Okay. Next and after that class B. Next class C, I'm taking. Next class D I'm taking sir. Have you observed? Class A, class B, class C, class D. Sir, how many classes are available? Can you please tell how many classes are there? Four classes are available. Which name I can use for this program? Because there is no public class. Because there is no public class, we can use any name for Java program, no problem at all. Any name you can use, no no restrictions right. That's why I want to save this name, this file as Durga.java, acceptable or not? Why not? Happily you can take, here just observe. I want to save, sir, I want to save this program, yes. I want to save this program as, here observe that, uh, Durga.java.

[6:51]I'm taking sir. Durga.java like this I'm taking. Have you observed? Sir, now which name I used, which name I used, if you observed at the bottom you can be able to see Durga.java. Are you getting at the bottom you can be able to see Durga.java right. Now, let me compile this code sir. Let me compile this code. Here, D colon Durga classes, here let me compile, Java C, Java C. What is the, what is the name of the program? Durga.java. I'm taking sir. Durga.java. Perfectly it's going to work, no problem at all sir. Yes, happily the code compiles fine, there is no syntactical mistake, that's why itself is acceptable right. Suppose if I declare B as the public. I want to declare B class as the public. If I declare B as the public, what will happen sir? Observe that if I declare B as public, as public, compulsory, name of the program should be B.java only. By mistake, if you are using any other name, immediately error we are going to get. Have you observed right? Class B, class B is the public, compulsory name should be B.java, but I used the name Durga.java. Are you getting I used the name Durga.java, that's why so take a bit special care. Now if I can ask the compiler, compiler will give left and right. Hey Durga, class B is the public, should be declared in a file name.

[8:28]Public class B. This is the compile time error we are going to get. Have you observed, class B is public, should be declared in a file name B.java. This is the compile time error we are going to get sir. So, to avoid this compile time error, I have to save this program as B.java. If I save this program as B.java, happily the code is going to compile. Have a look once. Have a look once. I'm trying to compile, I'm trying to save, yes. Save as B.java. B.java. I used which name sir? B.java. Now, I hope you can, you can be able to see, you can, you can be able to see what is the name I used? B.java. Are you getting? Now, let me compile this code B.java. Let me compile this code B.java. B.java, Java C B.java. Happily the code compiles fine. There is no problem at all. So, the important conclusion is a Java program can contain any number of classes, but at most one class can be declared as public. So more than one public class is not allowed. Because so public means outside person can access, so that outside person only one class should be executed. Remember this one. So a Java program can contain any number of classes, but at most one class can be declared as public. If there is, there is a public class, the name of the public class, name of the program must be matched. If there is no public class, if there is no public class, then we can use any name for Java program, no restrictions, right?

[12:02]Are you getting? So is it possible to declare more than one public class in the same program? No. By mistake, if you are trying to declare, immediately error by default we are going to get. Clear for all of you right? Sir, next, whenever I asked this question, are you which name I want to use for this program? Whenever I asked this question, most of the people are going to feel, sir, the class which contain main method, the class which contain main method. That class name we have to use sir for the file name. So, that class which contain main method and the name of the program, there is no relation at all. So then which two things are related, public class name and the name of the program, these two are related, but the class which contain main method and the name of the program, there is no relation at all. Even for that also I want to clarify a small point right. Here, listen carefully sir. Okay, well. Now, here just observe that. I want to take a small another program, just a matter of five minutes. Take very special care sir. I have one program is there. Class A. Are you getting I have one program class A. Public static void main method, string array acts. Now system.out.println, A class main. This is which class right? A, A class main. Now, class B, public static void main method, string array acts.

[13:53]I'm taking. Now system.out.println, B class main. Are you getting B class main? Okay. Sir, this is A class main, B class main. Now, class C I'm taking. Public static void main string array acts. I'm taking. Now system.out.println C class main, like this I'm taking sir. So, how many classes are there? Three. A class contain A class main method, B class main method, C class main method. One more class I'm taking, class D sir. Now observe. Total how many classes are there? Four classes are there. Now can you please tell this total is a single program only. Assume single program only. Can you please guide, which name I have to use for this program? Either A.java or B.java or C.java or D.java or any name?

[15:15]Huh, why are you thinking? Any name. Can I use any name? Yes, yes, you can use any name. No problem at all. What is the reason for that is, there is no public class. If there is no public class, any name you can use. Now, I want to use name of the program, yes, Durga.java. Perfectly acceptable. Are you getting? I'm taking Durga.java, perfectly acceptable right. Because there is no public class, are you getting? So the class which contain main method, name of the program, no relation at all. Remember this one. So any name you can use because there is no public class. Okay, well. Now I want to compile, then immediately you must ask, sir, your program contain these many classes. If every class contain main method, if I run this program, which main method has to execute? That thing you should have clear clarity. Here, I want to compile this code. Observe Java C, Java C, Durga.java.

[16:25]Can you please confirm how many dot class files will be generated? If I compile this code, how many dot class files will be generated? Very simple, in this how many classes are there? A, B, C, D, total how many classes are there?

[16:47]Four classes are available right. Now the important conclusion, the important point is whenever we are compiling a Java program, for every class present in that program, a separate dot class file will be generated. Remember, for every class present in that program, a separate dot class file will be generated. That's why so how many classes will be generated? A.class, B.class, C.class, D.class. Very unfortunate, Durga.class file is not going to be generated, because there is no Durga.class. Clear right? Now, here observe. A.class, B.class, C.class and then D.class. Sir, total how many classes are going to be generated? A.class, B.class, C.class, D.class, four classes are going to be generated. Okay? Now, if I run A class, because observe that we can compile a Java program. A Java program, Java source file, .java file, but we can run a Java class file, not program, a Java class file. Sir, now Java A sir. Can you please run A class. Whenever we are executing A class, A class main method will be executed. Remember, whenever we are executing A, which main method will be executed sir? A class main will be executed. Next if I run, if I run Java B. If I run Java B, which main method will be executed sir? B class main will be executed. Sir, if I run B class, B class main method. If I run A class, A class main method. If I run C class, C class main method. That's why. Java C, if I can take C class main. Are you getting C class main? Then immediately you may ask. Observe very carefully sir. If I can take Java D sir. Java D, D. Can you please tell is the D class contain main method? No. D class doesn't contain main method. Whenever we are executing a class, if that class doesn't contain main method, immediately we will get error. Are you getting boss? Main method is not available. Please declare main method as public static void main. Error we are going to get. Clear right? Suppose if I ask Java Durga. Java Durga, immediately, immediately error we are going to get. Hey, Durga.class file is not available. How you are going to ask me to run man? Durga.class file is not there. Again error saying no class found error or something like some error, saying class definition not available, .class file is not available like that. Are you able to understand? So, a Java program can contain any number of classes, where every class can contain main method. Remember, whenever we are executing a particular class, that class main method will be executed. If that class doesn't contain main method, error we are going to get. If that .class file is not available, then we will get error. Everyone can be able to understand right? Sir, please make sure this knowledge must be required. Sir, let me execute this code so that you people can feel more comfortable right. Observe, observe carefully. Let me take one separate program. Okay. Here I'm taking separate file I'm taking sir. Class A. Class A. What is this one? A class main, A class main method, like this I'm taking sir, class A, A class main method. Now, B class main, next C class sir. C class main method like this I'm trying to change the names. A class, B class, C class, D class are all sir, can you please tell how many classes are available? A class, B class, C class, D class. Can you please tell which name I have to give? Any name you can use because, because there is no public class. That's why here what I'm trying to take? Let me save this code yes, Durga.java. Are you getting? I want to save inside D colon Durga classes. Inside D colon Durga classes. I want to save this code yes, Durga.java. Okay, I like I'm taking sir, Durga.java. Now, I'm, I want to compile this code. Java C Durga.java. If I compile this code, how many dot class files will be generated? For every class, a separate dot class file will be generated. A.class, B.class, C.class, D.class. Okay. Sir, now let me compile this code. Java C Durga.java. How many? Four dot class file. If I execute Java A. If I execute Java A, A class main method, right. Okay. Now observe A class main method. If I execute Java B, B class main method. If I execute Java C, C class main method. If I execute Java D, D, can you please observe? D class doesn't contain main method. Are you getting D class doesn't contain main method? Immediately error we are going to get. Observe carefully right. Sir, Java D, error main method not found in class D. Please defined, yes. Public static void main like that we are going to get the error, right. Okay. Now my target is Java, Java Durga I'm taking. Sir, Durga dot class file is not there, because there is no class named with Durga. Durga dot class file is not there. What will happen sir? Error, could not load, could not find or load main class Durga. Are you getting? Here main method is not available, but here dot class file is not available like this it's going to tell. Okay? Please make sure you people should have clear clarity up to this. Sir, let me repeat matter of one or two minutes. A Java program can contain any number of classes, but at most one class can be declared as public. If there is no public class, we can use any name for Java program. If there is a public class, if there is a public class, compulsory name of the program and the name of the public class must be matched. Okay, next sir, we can compile a Java program. Whenever we are compiling a Java program, for every class present in that program, a separate dot class file will be generated. Are you getting that a separate dot class file will be generated? Suppose, sir, whenever we are executing a Java class. Sir, we can compile a Java program, dot Java file, but we can run a dot class file. Whenever we are executing any dot class file, the corresponding class main method will be executed. A class main, B class main, C class main like that right. If the corresponding class doesn't contain main method, then we will get error. Are you able to understand right?

[24:32]This is first thing what you people should aware about Java source file structure. Clear right?

Need another transcript?

Paste any YouTube URL to get a clean transcript in seconds.

Get a Transcript