static

0
  1. public class static1 {
        static int age;
        static{
            age =18;
            System.out.println("this is static keyword ");
            System.out.println(age);
        }
        public static void main(String[] args) {
            System.out.println("and this is  under in main method");
           
        }
       
    }
  2. //static kyword and non static member on class
    class Rakesh{
    static int age;
    static String Name ;
    int age1;
    String Name2;
    static{
        System.out.println("static keyword");
        age =10;
        Name = "Rakesh";
    }
    {
        System.out.println("none static keyword");
        age1=20;
        Name2 ="Rahul";
    }
    static void show(){
       
        System.out.println(age);
        System.out.println(Name);
    }
    void show1(){
        System.out.println(age1);
        System.out.println(Name2);
    }


    }
    public class static2 {
        public static void main(String[] args) {
        Rakesh obj =new Rakesh();

        //if you focous here static object execute without crating a object but normal blocked use new object
            Rakesh.show();
            obj.show1();

           
        }
    }
  3. import java.util.Scanner;

    class Shopkeeper {

        private static float saltRate;
        private static float kurkureRate;
        private static float mixtureRate;
        private static float sugarRate;

        private static float saltQuantity;
        private static float kurkureQuantity;
        private static float mixtureQuantity;
        private static float sugarQuantity;

        static {
           
            saltRate = 10;
            kurkureRate = 5;
            mixtureRate = 5;
            sugarRate = 40;

            saltQuantity = 0;
            kurkureQuantity = 0;
            mixtureQuantity = 0;
            sugarQuantity = 0;
        }

        static void input() {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter quantity of products:");
            saltQuantity = sc.nextFloat();
            kurkureQuantity = sc.nextFloat();
            mixtureQuantity = sc.nextFloat();
            sugarQuantity = sc.nextFloat();
            sc.close();
        }

        static void compute() {
            System.out.println("this rate for per kg and per pices");
            float totalValue = (saltRate * saltQuantity) + (kurkureRate * kurkureQuantity)
                    + (mixtureRate * mixtureQuantity) + (sugarRate * sugarQuantity);

            System.out.println("Total value of all products: " + totalValue);
        }
    }

    public class static3 {
        public static void main(String[] args) {
            Shopkeeper.input();
            Shopkeeper.compute();
        }
    }
  4. //static vs none static
    class chek {
        static int a;
        static int b;
        static {
            System.out.println("this is static element");
            a = 400;
            b = 20;
        }
        int m;
        int n;
        {
            System.out.println("this is none static element");
            m = 10;
            n = 12;
        }

    }

    public class static4 {
        public static void main(String[] args) {
            chek p = new chek();
            chek q = new chek();
            System.out.println(p.m);
            System.out.println(q.m);
            System.out.println(chek.a);

        }

    }

  5. class st {
        static void disp() {
            System.out.println("this is static methode");

        }

        void disp1() {
            System.out.println("this is none static methode");
        }
    }

    public class static5 {
        public static void main(String[] args) {
            // if here static methode call beacause object creation
            st.disp();
            st obj = new st();
            // this is none static methode call using object creation
            obj.disp1();
            // and one thing care fully remember static can be use with object creation but
            // this is not good practice
            obj.disp();

        }
    }

 

Post a Comment

0Comments
Post a Comment (0)