Object Oriented Programming - JAVA- class boundary, variable types, modifiers, java main method

Object Oriented Programming - JAVA👽

There are certain aspects of Object Oriented Programming. So when we use OOP we need to know about those elements. In this blog, Let's discuss about class, variables, modifiers and methods & main method .


Class....

A class is a blueprint that defines common variables and methods for objects of a particular type.
A class has a boundary. Inside this boundary we only have the class, constructor and main method.
Even if a method is in the class, we cannot take it to the class boundary.


Variables....

Variables  are used to store information to be referenced and manipulated in a computer program. There are two variable types.
  • Static variable
  • Non - static variable

Static variable

The easiest way to find static variable is to find out if there is a static key ward. 

    ex :       static int i ;

These can be accessed anywhere within the class. It can be accessed even without creating an object.

Non - static variable

A non-static method in Java does not have the keyword 'static' before the method name. A non-static method belongs to an object of a class. There are two variable types.
  • Local variable
                    A variable inside a method is called a local variable. It can never be a static variable.
                    ex :       void print (){
                  int y = 52 ;
              }
  • Instance variable
                     A variable inside the class boundary is called a instance variable. It can be a static variable.
                    ex :       public static void main (String [] args){
                  int x = 52 ;
              }

let's see an example to identify static and non-static variables.

          class Java {

            static String x = "A";     

            void print (){
               String y = "B";     
               static int a = 56; 
            }

            public static void main (String [] args){
               int z = 45;     
            }
          }

Static variable
Local variable
Instance variable

Modifiers....

There are modifiers into two groups.
  • Access Modifiers 
  • Non-access modifiers 

Access Modifiers

In OOP we find 4 access modifiers.
  • default  - Code is only accessible to classes in the same package.
  • public   - The code is accessible for all classes.
  • private  - The code is only accessible within the declared class.
  • protect  - The code is accessible in the same package and subclasses. 

Non - access Modifiers

There are mainly 3 non-access modifiers.
  • static  - Can be used anywhere in the class and can call without object or access. 
  • abstract   - Can only be used in an abstract class, and can only be used on methods. 
  • final  - Attributes and methods cannot be overridden/modified.

Main Method....

Main method is first loaded in the code.

Comments

Post a Comment

Popular posts from this blog

Sorting Algorithms - JAVA👽

Object Oriented Programming - JAVA- Method Overriding