Articles by "Static data"
Showing posts with label Static data. Show all posts
Java Notes For Java Beginner

Access specifiers

Java have four access specifiers.
  • public :
    • The keyword which is used a access specifier whose data can be accessible in same class, other class, other program, other folder, other colon and even other machine.
    • Java follows client-server architecture, so main() method should be public.
  • private :
    • The stronger access specified which restricts the data to be come out of class.
    • Private data can not be accessible even through it's class object.
  • protected :
    • The partial public and partial private access specifier.
    • It follows public property as the data can be come out of the class but only in inheritance.
    • It private property can not be accessible through class object.
  • default :
    • It is not keyword.
    • It works same as public up-to one working folder.
    • Otherwise it becomes private.


Static

Static keyword is used to load the main method into primary memory by JVM[Java Virtual Machine].

void

This keyword is used as a return type of method is not going on return any value.

main

A flag which is used as indicator of starting point of program for compiler and inter-printer.
Java Notes For Java Beginner

Static keyword is used to load the class data into primary memory first to the object. As it is directly loaded into memory, it can be accessible directly.

Note : Static data is shareable to all the objects of same class.

Write a program to access static & non-static data of class.
class class

{
    int x; //non-static
    static int y; //static
    public static void main (String args[])
    {
        c c1=new c();               //New object created [1st]
        system.out.println(c1.x);   //called using c1 object
        system.out.println(c1.y);   //called using c1 object
        system.out.println(c.y);    //called using class
        system.out.println(x);      //directly called
 
        c1.x++;                     //Operation performed using c1 object [Value becomes x=1]
        c1.y++;                     //Operation performed using c1 object [Value becomes y=1]
       
        c c2=new c();               //New object created [2nd]
        system.out.println(c2.x);   //called using c2 object
        system.out.println(c2.y);   //called using c2 object
    }
}


Output:
           0
           0
           0
           0
           0
           1

Static data can be accessible by three ways.

  1. Using class object.
  2. Using class name.
  3. Directly.

Note : Non-static data can be accessible only through it's class object.

System.out.println() //Statement

System is a built in class and out is static field of it. The combination of this two will send the data from editor to keyboard memory. This memory is the space reserved on primary memory for every java application. The println() method of print stream class sends the data from keyboard memory to console.

PrintStream class have two methods

1> println(): This method send the corsor to the next line after printing the data.

Example:
class D
{
    public static void main(String args[])
    {
        System.out.println("\t\t Hello\t\t\t");
        System.out.println("How Are You?");
        System.out.println("Bye...");
    }
}

Output:
         Hello
    How Are You?
    Bye...

2> print(): It remains cursor on the same line after printing.


Example:
class add
{
    public static void main(String args[])
    {
        int a,b,c;
        a=10;
        b=20;
        c=a+b;
       
        System.out.println(C);
        System.out.println("Addition is "+c);
        System.out.println("Addition of "+a" and "+b" is "+c);
        System.out.println(a+" + "+b+" = "+c);
    }
}

Output:
    30
    Addition is 30
    Addition of 10 and 20 is 30
    10 + 20 = 30
   
Note : + sign is used as separator in system.out.println statement...