Access Control

Encapsulation provides access control to avoid misuse means makes the control which can use the members of class.

This can be done by using access specifiers to the members those are public,private,protected and default also.

public- can access out side of the class means any where in the program that's why main is public.

private- can access by only it's members of that particular class means can not accessed by outside of class.

default- is acts like public for within the package but can not accessed out side of the package.

protected- used in Inheritance concept.

//example for access specifiers
class AccessSpecifiersExample
{
  public int a;       //public
  int b;              //default
  private int c;      //private

  void setC(int c)    //we can set only by using member of the class
  {
    this.c= c;
  }
  int getC()
  {
    return c;
  }

}

public class Demo
{
  public static void main(String args[])
  {
    AccessSpecifiersExample as=new  AccessSpecifiersExample ();

    as.a=10;
    as.b=5;
    //as.c=15;  it will throws an error

    as.setC(15);

    System.out.println("value of a is::"+as.a);

    System.out.println("value of b is::"+as.b);

    System.out.println("value of c is::"+as.getC()); // we can get by only its member 
  }
}

Output:

value of a is::10
value of b is::5
value of c is::15

It is good to use private access specifier as its provides the hiding of data from outside of the class.

Static

By using Static keyword we can use members of a class without initializing the objects to that class and it called before object exist.

main()is the best example for static method because it have to execute before it's objects created.

The instance variables which declared as static are acts like global variables as they can access by every object with the same values thus there is no need of individual copy of these variables.

Methods declared as static have several restrictions:

• They can only directly call other static methods.

• They can only directly access static data.

• They cannot refer tot his or super in any way.

static block

It is the block which initializes the static variables but it will execute only once when class first loaded.

// Demonstrate static variables, methods, and blocks.
class UseStatic 
{
 static int a;
 static int b;
 static void meth(int x) 
 {

 System.out.println("x = " + x);
 System.out.println("a = " + a);
 System.out.println("b = " + b);
 }
 static  //initializing static variables and first it will execute.
 {
 System.out.println("Static block initialized.");
   a=4;
  b = a * 4;
 System.out.println("In static block");
   meth(30);
 }

}
public class StaticDemo
{
  public static void main(String args[])
  {
    System.out.println("In another class");
    UseStatic.meth(42);   //calling in another class with the reference of class name.
  }
}

Output:

In another class
Static block initialized.
In static block
x = 30
a = 4
b = 16
x = 42
a = 4
b = 16

final

  • A final is a keyword which prevents from modification to make essentially constant. .
  • So we have to initialize the final fields when they declared or by using constructors .
  • This can be used to variables, parameters, local variables and methods.

variables

final int FIRST_VARIABLE = 123;

final double SECOND_VARIABLE = 342.0;

Capital letters are used to declare variables as final.

Parameters

Prevents the method to change the value of parameters passed to it.

Local variables

Prevents to change the value of local variables by that block of code.

Length-instance variable of array

length is an attribute of array which holds the size of the array not the size which is in use.

// example for the length array member.
public class Length 
{
 public static void main(String args[]) 
 {
 int a1[] = new int[10];
 int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
 int a3[] = {4, 3, 2, 1};
 System.out.println("length of a1 is " + a1.length);
 System.out.println("length of a2 is " + a2.length);
 System.out.println("length of a3 is " + a3.length);

   System.out.println("elements in array a1");
   for(int i=0;i<a1.length;i++)
     System.out.print(a1[i]+" ");

   System.out.println();

   System.out.println("elements in array a2");
   for(int i=0;i<a2.length;i++)
     System.out.print(a2[i]+" ");

   System.out.println();

   System.out.println("elements in array a3");
   for(int i=0;i<a3.length;i++)
     System.out.print(a3[i]+" ");
 }
}

Output:

length of a1 is 10
length of a2 is 8
length of a3 is 4
elements in array a1
0 0 0 0 0 0 0 0 0 0
elements in array a2
3 5 7 1 8 99 44 -10
elements in array a3
4 3 2 1

Nested and Inner Classes

  • We can declare a class inside a class which is called Nested classes.
  • Scope of the nested class is bounded by the scope of the enclosed class .

Ex:

if class B is nested class enclosed by class A, Then

B can access the members of A.

But A can not access the members of B.

Two types:

Static- specified by static keyword means the nested class can access the only static members of the enclosing class otherwise we have to declare a object to that class to use non-static variables.

Non-static (or) Inner class-An inner class is the non-static nested class. In this all the members of the outer class can be accessed by nested class.

// example for an inner class.
class Outer 
{
 int outer_x = 100;
 void test() 
 {
 Inner inner = new Inner();                           //to access inner class members
 inner.display();
  System.out.println("inner class variable::"+inner.inner_x);
 }

  void display1()
  {
    System.out.println("This is in outer class");
  }

  // this is an inner class
 class Inner 
 {
   int inner_x =555;

   void display() 
 {
 System.out.println("display: outer_x = " + outer_x); //access outer class members
   display1();
 }
 }
 /* void display2()
  {
    System.out.println("It will not execute"+inner_x); //it will throw an error as we can't use inner class members by outside class members directly.
  }
  */
}


public class InnerClassDemo 
{
 public static void main(String args[]) 
 {
 Outer outer = new Outer();
 outer.test();

 }
}

Output:

display: outer_x = 100
This is in outer class
inner class variable::555

  • we can define inner class within a block or even within loop also.

String Class

  • A String is a class which creates and holds a string as object.

  • Strings are immutable means we can't change once it initialized.

To change we can

  • create a new String object with modifications.

  • using peer classes like StringBuilder and StringBuffer which allows us to do modifications.

//example for String class operations
 public class StringDemo
{
  public static void main(String args[])
  {
    String str1= "Hello"; //declaring a string
    String str2 = "welcome";
    String str3 = str1 +" "+ str2 +" to java"; //+ operator is to concate two strings.

    System.out.println(str1);//displying
    System.out.println(str2);
    System.out.println(str3);

    System.out.println("length of string 1 is::"+str1.length()); // length() returns the length of the string. (int length())

    System.out.println("character at index 4 of string 2 is::"+str2.charAt(4)); // char charAt(int a) will returns the character at a particular index starts with 0

    System.out.println("String 1 and String 3 are same or not::"+str1.equals(str3)); //boolean equals(String ob) return ture if same false if not.

  }
}

Output:

Hello
welcome
Hello welcome to java
length of string 1 is::5
character at index 4 of string 2 is::o
String 1 and String 3 are same or not::false

Command-Line Arguments

This allows us to pass the information to program when it is running.

This is done by main()method which takes a array of String as an argument.

The command line argument will follows by the program name in command line.

The first command line stored in args[0] and second is in args[1]...

// Display all command-line arguments.
public class CommandLine 
{
 public static void main(String args[]) {
 for(int i=0; i<args.length; i++)
 System.out.println("args[" + i + "]: " +
 args[i]);
 }
}

Output:

Hello welcome to java (command line)

args[0]: Hello
args[1]: welcome
args[2]: to
args[3]: java

results matching ""

    No results matching ""