Scoping and Variablelength Arruguments

 public class HelloWorld{


     public static void main(String []args){

        System.out.println("Hello World");

int b = 200;

      sum();

     }

     static void sum(){

         int a  = 100;

         

         System.out.println(a);

     }

}



function scoping jasa ya sum() wala method ka under hum b ko access nahi kar sakta because wo function scope ka bahar haa  sum(){ } 

Block level scope { }

public class HelloWorld{

     public static void main(String []args){

        System.out.println("Hello World")

        {

            int c = 100;

        }

        System.out.println(c);

     }

}

asa karana ga toa error aya ga because hum block ka bhar c ko print karwana ke koshis kar raha haa

error  will  be symbol not found 


public class HelloWorld{


     public static void main(String []args){

        System.out.println("Hello World");

        

      

      String a = "bip";

      

      {

          a = "soniya";

      }

      

        

        System.out.println(a);

       

     }

}


bus kuch nahi hum refrence ko update karwa deraha haa 





Shadowing 


public class HelloWorld{
    
   static int x = 100;

     public static void main(String []args){
         
        System.out.println(x);
        
        {
            int x = 200;
            System.out.println(x);
        }
        
        
        System.out.println(x);

     }
}


agar hum higher level pae x ko define kara or fir sa x ko block level ya function level mea define kara 
toa vo huma error nahi dega or hamara output be like 

100
200
100

because block level mea hiher wala hide ho jata haa iseko shadowing khata haa


Variablel length Arguments



import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
        
        arr(20,3,4,5,6,2);
     }

     static void arr(int ...num){
         System.out.println(Arrays.toString(num));
     }
}


In this variable-length Argument mae we will give so many numbers and it will give output  like an array.

variable length arguments :- length is not constant it varies over the period 


import java.util.*;
public class HelloWorld{
     public static void main(String []args){
         multi(2,3,"nkd","hihi","jkk","sojs");
     }
     static void multi(int a,int b, String ...c){
         System.out.println(a+" "+b+" "+Arrays.toString(c));
     }
}

multiple argument is also possible 






























Comments