Static Block

  1. class Sb
  2. {
  3. static int x = 20;
  4. static int y = 30;
  5. static
  6. {
  7. System.out.println("Static Block Invokes");
  8. }
  9. }
  10. class Tsb
  11. {
  12. public static void main(String...a)
  13. {
  14. System.out.println(Sb.x);
  15. System.out.println(Sb.y);
  16. }
  17. }
  18. Output:
  19. Static Block Invokes
  20. 20
  21. 30
  1. class Temp
  2. {
  3. void show()
  4. {
  5. System.out.println("method of Temp ");
  6. }
  7. }
  8. class Demo
  9. {
  10. static int x=20;
  11. static int y=30;
  12. static
  13. {
  14. Temp t = new Temp();
  15. t.show();
  16. }
  17. }
  18. class Xy
  19. {
  20. public static void main(String...a)
  21. {
  22. System.out.println(Demo.x);
  23. System.out.println(Demo.y);
  24. }
  25. }
  26. Output:
  27. Temp.java:27: error: class, interface, or enum expected { ^ 1 error
  1. class Temp
  2. {
  3. void show()
  4. {
  5. System.out.println("method of Temp ");
  6. }
  7. }
  8. class Demo
  9. {
  10. static int x=20;
  11. static int y=30;
  12. static
  13. {
  14. System.out.println(x);
  15. System.out.println(y);
  16. System.out.println("HELLO");
  17. }
  18. }
  19. class Xy
  20. {
  21. static
  22. {
  23. Temp t = new Temp();
  24. t.show();
  25. }
  26. }
  27. Output:
  28. Error: Main method not found in class Xy, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
  1. class Temp
  2. {
  3. void show()
  4. {
  5. System.out.println("method of Temp ");
  6. }
  7. }
  8. class Demo
  9. {
  10. static int x=20;
  11. static int y=30;
  12. static
  13. {
  14. System.out.println(x);
  15. System.out.println(y);
  16. System.out.println("HELLO");
  17. }
  18. }
  19. class Xy
  20. {
  21. public static void main(String...a)
  22. {
  23. Temp t = new Temp();
  24. t.show();
  25. }
  26. }
  27. Output:
  28. method of Temp

| Copyright ©2016 | All Rights Reserved |
| Design by Uves Khan |

Free Web Hosting