Advertisement
rangga_hrdme

SCOPE

Apr 19th, 2021
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. public class Scope_var {
  2.  
  3. // Global
  4.     public static String Name = null;
  5.  
  6.     public static void main(String[] args) {
  7.         MyName();
  8.         Testing();
  9.     }
  10.  
  11.     static void MyName() {
  12.         Name = "Eren Yeager";
  13.         // local
  14.         var car = "Datsun";
  15.         System.out.println("Name: " + Name + " car: " + car);
  16.     }
  17.  
  18.     static void Testing() {
  19.         var a = 10;
  20.         if (a == 10) {
  21.             int b = 5;
  22.             System.out.println("b: " + b);
  23.         }
  24.         // System.out.println("b: "+b);
  25.         // Error: out of scope/ coverage
  26.     }
  27. }
  28. /* Variable Scope = coverage of variable
  29. * Scope: Global & Local
  30. * */
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement