Java OOPC -Class, Variables, Methods, Modifiers ,Main Methods

 

Class

Boundary of a class means that in a class except the methods, other whole things we tell  it boundary of a class.

Class, constructor, and main method comes under the class boundary. Other methods also inside of the class but we did not consider it as boundary of the class.




in here according to the above coding except the 'testmethod' comes under the boundary of the class.

                                   

Variables

 

In Java, variables are containers for storing data values. Each variable has a specific data type, which determines the kind of data it can hold.

 

There are two main data variable types.

1.static variables

2. non-static variables

 




Static Variables

Using ‘static’ keyword these variables are created , and can access in any place in the inside of the class.

It is fact that the Local variable cannot be a static variable.

 

Non Static Variables

Non static variables can divide into two as local and instance.

  1.   Local variable

  - The variable that inside the Method is called Local variable.

  - Not visible or accessible outside the method, constructor, or block in which they are defined.

 

    2.   Instance variable

 - The variable that on the class boundary.

 -  Declared within a class but outside any method, constructor, or block.




Java Methods

Java Methods divide mainly in two types as Predefine Methods and User-define Methods.

 

1.Predefine Methods:

Also know as build-in or standard library methods.

In the JDK there are methods already inbuild for the usage of programmers. We called those Methods as  Predefine Methods.


Eg. get(),set(),print(),max()

 

 

2.User-define Methods:

Created by the programmer to perform specific task or actions. These Methods are defined with the class.

User-define Methods enhance code reusability, modularity, readability.


Eg. eat(),run(),display()

  






Java Modifiers


In Java, modifiers are keywords that are used to specify the properties and behaviors of classes, methods, variables, and other program elements. There are several types of modifiers in Java, categorized into different groups:

 Mainly as Non access modifiers and Access modifiers.

 

Non access modifiers

 These modifiers provide additional information about the structure or behavior of the code.

 

Eg: static

      Final

      Abstract


static- Can use or access in any place of class and without object can call it. 

 

Access modifiers

- Determine the visibility of classes, methods, and variables.

- Four access modifiers are available in Java:

 

  1. default  :Accessible only within the same package.

Eg.

    int defaultVar; /default int defaultVar;

    default class defaultclass{}/class defaultclass{}

    void defaultone(){}/default defaultone(){}

 

      2.  private   :Accessible only within the same class.

Eg.

Private int privatevar;

Private class privateclass{};

Private int plus(){};

 

     3.  public     : Accessible from any other class.

Eg.

Public  Int privatevar;

Public class privateclass{};

Public int plus(){};

 

     4. protected:   Accessible within the same package and by subclasses (even if they are in a                                       different package).

    Eg.

Protected  Int privatevar;

Protected class privateclass{};

Protected int plus(){};

 

Sub class and super class

In object-oriented programming, the terms "subclass" and "superclass" are used to describe the relationship between classes.









Main method

 

  • This the gate point of Java program.
  • The main method must declare as follows.

 

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

 

In here several components of that contain say several things,

 







public  

 This is access modifier. Main method should be public because it give access to any class of the project.

 

static 

Non access modifier. Help to access in any place in the class. Either have object or not have object it can be accessed.


Void 

Default return type and main method does not return any value.


Main

Name of the main method


(String[] args)

Declare parameters which are input.


String

String is a class but in here using it as data type. It help to input any type of data.


[]

It helps to store and arange data as a array.


Args

parameter is an array of strings that can be used to pass information to the program when it is run.


{}

Opening and closing curly brackets.



 

 


Comments

Popular posts from this blog

Linked List Data Structures

Java OOPC -Encapsulation

Java OOPC - Interface