If we want to execute the program in java compulsory jvm always starts the execution from the main method.
whenever the programmer asks the jvm to run the program jvm always looks for main method.
main method is the starting point for the program.
If we dont define main method immediately we will get compile time error.
If there is no main method we cannot execute the program
JAVA Source file structure:
Source file is nothing but where we are going to write our programs
1. In a single source file we can have multiple classes/ class files
>>> class A1{
------
}
class A2{
-------
}
classA3{
-------------
}
2. If no class is declare as public then we can save that file with any name
A1.java
A2.java
A3.java
A4.java
Hello.java
Anything.java
3. It is a good programming practice that declare one class as public, the class which we declared as publice the public class and filename must be same otherwise we will get compile time error.
>>> class A1{
------
}
public class A2{
-------
}
classA3{
-------------
}
Ex: A2.java ===>valid
A1.java ===>invalid
A3.java===> invalid
Hello.java ===>invalid
4. If we are trying to define more than one class as public immediately we will get compile time error
In a single source file we should not take more than one class as a public.
>>> class A1{
------
}
public class A2{
-------
}
public classA3{
-------------
}
// we will get compile time error
5. The class which we declare as a public in that public class only we must and should take main method.
>>> class A1{
------
}
class A2{
-------
}
public classA3{
public static void main(String[] args)
{
-------------
}// end of main
}// end of class
Benifits of ide/eclipse:
We need to focus on
1. perspective(on what perspective u want to write the programs)
2.eclipse editor (where we are going to write our programs
3. console (where we can see the output)
4. project explorer/package explorer (where we are going to manage our projects)
5. git
6. debug
Eclipse:
right side click on + symbol
select java(default)
firse we need to create a project
goto file--->new--->project--->java project--->next---> projectname and version--->next--->finish
project got created
1.jre system library
2. src (source folder where we can write our programs)
second way to create project :
goto window---> show view--->click on console---> window--->show view--->package/project explorer
Whenever we create a project two files are going to be created automatically
Assume that the Flipkart application
we have three modules
1. men-------> 25 classes
2. women----> 20 classes
3. kids-------->45 classes
under the men module, we need to write 25 classes like that
src---> class names like men1,men2,women1,women2,kids1,kids2 etc
we have taken 90 classes
if we want to execute only kids classes then we need to look for those classes in 90 classes
if we want to look only men classes then we need to look for men classes out of 90 classes
its become complex
for example under kids module if we have different types of chacolates like 100 5star,150 kitkat,200 dairymilk, etc
if all are combined together in one box.
instead of that if we put them separately in boxes its very easy to get them.
in the same way dont write all your programs under src folder.
under src folder create packages
package is a collection of classes + interfaces+enums.
Standard syntax for package:
package <packagename>
package is a fixed keyword inorder to convey it as a package
pacakagename: can be anything but it should be very meaningful.
package men;
inside this men all the program related to men
package women;
package kids;
But this above are not valid in real time we have some standard sysntax:
domain name in reverse. modulename. submodulename
www.flipkart.com
package com.flipkart.men;
src---> new--->package
give some name like
package com.flipkart.men;
package com.flipkart.women;
package com.flipkart.kids;
now to write all men program select this package--->new-->class---> give class name
how many classes we can write all classes
men1,men2,men3,women1,women2,women3,kids1,kids2,kids3,....... etc...
to execute kids class simply look for kids package
To run: rightclick--->run as java application
package com.flipkart.kids;
public class kids3{
public void hello() {
System.out.println("hello");
}
public void job(){
System.out.println("job");
}
public static void main(String [] args) {
System.out.println("hello");
}
}
No comments:
Post a Comment