- Get link
- X
- Other Apps
Java class
It is a logical way to group together fields that hold values(data) and associated methods that operate on those fields into a single unit. A class defines structure and blueprint for creating objects. A java program must have at least one class.Java program starts with a class.All code is written within a class.
Class is unit which includes all variables,constants and methods.We can think of class as an group of all objects like school has a class, which groups all students,although each student has different properties.
A class has two parts:
- class header :- class header declares the name of class and as well as the name of superclass and interfaces which class inherits and implements.It can also have access modifiers which specify its scope like, public, private etc.
- class body:- class body contains the definition of fields, methods ,constructors and other declarations.It contains the code within curly braces{}.
We can define a class in java by using following syntax:-
This is simple class. A class definition starts with class keyword. It must be in small letter.You can name your class as per your requirement.
Tips:
- Class name should be written in mixed case ,so that each word's first letter is capital like, MyFirstClass.
- Name a class in a way that it is readable and understandable like, other person can understand purpose of class by reading its name only.
Program Explanation:-
We wrote our first java program in previous post, lets understand the program.
- First statement :- declares class named as MyFirstProgram.It is class header.
- Second statement :- starts the class body by opening curly brace.
- Third statement :- contains main method declaration. Lets understand it in detail.
Main() Method
It is method which invoke the execution of program. If we do not write this method, compiler will not start compilation and a compilation error will occur.
Main method contains following keywords:-
- public:- It is access modifier.It makes the main method public so JVM can access it from anywhere.Access modifier specify the scope of methods, variables etc. Public means having a global scope.
- static:- The static keyword is used so that main method can be invoked from anywhere.The static keyword is very powerful keyword.Static methods cannot be overridden and changed. Static means to be in a constant state and do not change.It is used with main method , so that the JVM can call it without creating an object.
- void:- void is a return type.It means this method does not return a value.
- main(String[] args):- main() is method which takes a String array a parameter. The main method must contain only string array as parameter.In the String[] , first letter must be capital.args is the name of parameter, it is not important to write args each time,it can have any name like a,str etc.
- fourth statement :- It contain main() method's opening brace.Each class,method, interface, enum and other definitions in java start and ends with curly braces.
- fifth statement :- It contains statement that prints the "Hello World!" on screen.This statement contains following parts.
- System:- It is the name of the standard class that contains objects that encapsulates standard I/O devices for your system.It is a class of java.lang package.The java.lang is the only package that does not require an import in java programs.
- out:- The object out represents the standard output stream and is static data member of the class System.
- println():-The println() is the method of out object that takes string as an argument and display it on the screen. It terminates that output lines so that each output displays in new line.
- Last statements:- These contains closing braces for main method and class.
I hope this post helps you in understanding the java class and main method. Try writing more programs.Happy Coding!
Comments
Post a Comment
If you have any doubt, ask here