ASSIGNMENT NO.2 - sidramaqsood/learning GitHub Wiki

1.MODIFIER

Modifiers are keywords that you add to those definitions to change their meanings. The Java language has a wide variety of modifiers, including the following:

Java Access Modifiers

Non Access Modifiers

To use a modifier, you include its keyword in the definition of a class, method, or variable. The modifier precedes the rest of the statement, as in the following examples (Italic ones) −

public class className { // ... } private boolean myFlag; static final double weeks = 9.5; protected static final int BOXWIDTH = 42; public static void main(String[] arguments) { // body of method } Access Control Modifiers: Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:

Visible to the package, the default. No modifiers are needed.

Visible to the class only (private).

Visible to the world (public).

Visible to the package and all subclasses (protected).

Non Access Modifiers: Java provides a number of non-access modifiers to achieve many other functionality.

The static modifier for creating class methods and variables

The final modifier for finalizing the implementations of classes, methods, and variables.

The abstract modifier for creating abstract classes and methods.

The synchronized and volatile modifiers, which are used for threads. 2.CONSTRUCTER Constructor in Java is block of code which is executed at the time of Object creation. But other than getting called, Constructor is entirely different than methods and has some specific properties like name of constructor must be same as name of Class. Constructor also can not have any return type, constructor’s are automatically chained by using this keyword and super. Since Constructor is used to create object, object initialization code is normally hosted in Constructor. Similar to method you can also overload constructor in Java. In this Java tutorial we will some important points about constructor in Java which is worth remembering for any Java programmer. It’s also worth remember that any static initializer block is executed before constructor because they are executed when class is loaded into memory while constructors are executed when you create instance of any object e.g. using new() keyword.

PUblic class ConstructorDemo{ public ConstructorDemo(){ System.out.println("Inside no argument constructor"); }

public ConstructorDemo(String name){ System.out.println("Inside one argument constructor in Java with name: " + name); }

public static void main(String args[]) throws IOException {

 ConstructorDemo d = new ConstructorDemo(); //calling no argument constructor in java
 ConstructorDemo e = new ConstructorDemo("Testing"); //calling one argument constructor in java

} }

Output: Inside no argument constructor Inside one argument constructor in Java with name: Testing