Class in Java

In this section, we will discuss about java classes and its structure. First of all learn: what is a class in java and then move on to its structural details.

Class in Java

In this section, we will discuss about java classes and its structure. First of all learn: what is a class in java and then move on to its structural details.

Class in Java

Class in Java

     

In this section, we will discuss about java classes and its structure. First of all learn:  what is a class in java and then move on to its structural details. 

Class: In the object oriented approach, a class defines a set of properties (variables) and methods. Through methods certain functionality is achieved. Methods act upon the variables and generate different outputs corresponding  to the change in variables.

Structure for constructing  java program:

package package_name;

//import necessary packages

import
package_name.*;

Access Modifier
class class_name{
member variables;
member methods;

public static void main(String[] args) {
   ...
   ...
.

   }

}

package: In the java source file you generally use the package statement at header of a java program. We use package generally to group the related classes i.e. classes of same category or related functionality. You have to save your java files in the folder matching the package name as mentioned  in java program.

import : Import keyword is used at the beginning of a source file but after the package statement. You may need some classes, intefaces defined in a particular package to be used while programming so you would have to use import statement. This specifies classes or entire Java packages which can be referred to later without including their package names in the reference. For example, if you have imported util package (import java.util.*;) then you need not to mention the package name while using any class of util package.

Access Modifiers : Access modifiers are used to specify the visibility and accessibility of a class, member variables and methods. Java provides some access modifiers like: public, private etc.. These can also be used with the member variables and methods to specify their accessibility. 

  1. public keyword specifies that the public class, the public  fields and the public methods can be accessed from anywhere.
  2. private: This keyword provides the accessibility only within a  class i.e. private fields and methods can be accessed only within the same class.
  3. protected: This modifier makes a member of the class available to all classes in the same package and all sub classes of the class. 
  4. default : Its not a keyword. When we don't write any access modifier then default is considered. It allows the class, fields and methods accessible within the package only.

static keyword  indicates that this method can be invoked simply by using the name of the class without creating any class-object.
void keyword specifies that this method will not return any type of data. 
main() method is the main entry point of the program, to start execution. First of all JVM calls the main method of a class and start execution .  JVM (Java Virtual Machine) is responsible for running java programs.
args is a string array that takes values from java command line. It's index starts from '0'. We can access values by writing args[0], args[1] etc.
println() function prints the output to the standard output stream (monitor).
out
represents the standard output stream (monitor).