java assignment - HinaUmer123/java-assignment GitHub Wiki

Local variables:

Local variables are declared in methods, constructors, or blocks. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block. Access modifiers cannot be used for local variables. Local variables are visible only within the declared method, constructor or block. Local variables are implemented at stack level internally.

Class/static variables:

Class variables also known as static variables are declared with thestatic keyword in a class, but outside a method, constructor or a block. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static. Constant variables never change from their initial value. Static variables are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants.

Showing Local Variable

public class LocalVar {   public static void main(String args[]) {     int localvariable = 101;     System.out.print(localvariable);   } }

Showing the Class Variable

public class ClassVar {   private static int classVariable;

  public static void main(String args[]) {     classVariable = 101;     System.out.print(classVariable);   } }

Types of java constructors

There are two types of constructors: Default constructor (no-arg constructor) Parameterized constructor

Default constructor

A Constructors that have no any parameters is known as default constructor. Why use parameterized constructor?

Parameterized constructor

it is used to provide different values to the distinct objects.  

public access modifier

Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

private access modifier

The private (most restrictive) fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods or constructors declared private are strictly controlled, which means they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.