Java non access modifiers


Published on

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java non access modifiers

  1. 1. Java Non Access Modifiers SCJP/OCJP exam objectives – 1.1,1.3,1.4 By, Srinivas
  2. 2. Non Access Modifiers • final • static • abstract • strictfp • native • synchronized • transient •
  3. 3. final • final can be applied for classes, methods, instance variables, local variables. • A class marked as final cannot be extended. • A method marked with final cannot be overridden. • A primitive data type or an object reference will not change its value or object when marked
  4. 4. static public class Ford{ int maxSpeed; public Ford(int maxSpeed){ Ford f = new Ford(100); this.maxSpeed = maxSpeed; } Ford a = new Ford(200); public void move(){ Ford e = new Ford(150); int speed = 0; while(speed<maxSpeed){ System.out.println(“Racing with speed”+speed); speed++; } function f.move(); } of method a.move(); public void printCarInfo(){ e.move(); varies from Object System.out.println(“Car name : Ford“); to object System.out.println(“ Car weight : 230”); f.printCarInfo(); System.out.println(“Engine Capacity: 3000cc”); a.printCarInfo(); Same output } e.printCarInfo(); }
  5. 5. static Virtual Memory .Class File Members marked as static belong to the Class file and not the
  6. 6. static • static can be applied for methods and variables. • A method or variable marked as static belongs to class file. • A static member should be accessed using the class
  7. 7. Accessing static members • static members can be accessed using the class name. E.g., public class Ford{ public static void printCarInfo(){ System.out.println(“Car name : Ford“); System.out.println(“ Car weight : 230”); System.out.println(“Engine Capacity: 3000cc”); } } Ford.printCarInfo();
  8. 8. Accessing static members public class Ford{ public static void printCarInfo(){ System.out.println(“Car name : Ford“); System.out.println(“ Car weight : 230”); System.out.println(“Engine Capacity: 3000cc”); } } public class FordShowRoom(){ public class FordShowRoom(){ public void getCarInfo(){ public void getCarInfo(){ Ford f = new Ford(); Ford.printCarInfo(); f.printCarInfo(); } } } }
  9. 9. static - rules • The static variable and static methods are called class members. • A method marked as static can only access other static methods and variables directly. • To access the instance variables and methods, a static method should have an instance on which the object members should be invoked. • Any instance can access the static variables and can change them. But, the changes will reflect for all the objects. • Members marked as static can be
  10. 10. static -
  11. 11. public static void main(String[] args){ }
  12. 12. abstract public class Car{ public void move(){ System.out.println(“Moves at max 40 kmph”); } } Benz Ford BMW Ferrari
  13. 13. abstract public abstract class Car{ public abstract void move(); } public class Ford extends Car{ public void move(){ System.out.println(“Move at 120 kmph”); } } public class Benz extends Car{ public void move(){ System.out.println(“Move at 200 kmph – Comfortably ”); } }
  14. 14. abstract - rules • abstract can be applied for classes and methods only. • When a method is marked as abstract – It should not have implementation. E.g., public abstract void move(); • Abstract methods should end with ‘;’ and not with ‘{ }’
  15. 15. abstract - Rules • When a method marked as abstract, the whole class should be marked as abstract. • A class can be abstract with out any abstract methods in it. • We cannot create an instance of abstract class. • An abstract method should be overridden in the subclass or should be marked as
  16. 16. abstract - rules • Abstract classes can have concrete(non- abstract) methods in it. • Abstract methods cannot be marked as final. • Abstract classes cannot be marked as final. • Abstract methods cannot be static. • Abstract methods cannot be
  17. 17. abstract - rules • Cannot create an instance of abstract class??? – An abstract class can contain abstract methods which does not have functionality and if we can create objects, we do not have functionality in abstract methods. – So, abstract classes are incomplete and are not eligible for creating
  18. 18. strictfp • strictfp can only be declared for methods and classes. • When declared, the code inside a class or method will conform to IEEE754 standard which makes the methods or classes behave in a platform independent way regarding the floating points. • strictfp cannot be used with
  19. 19. native • native modifier can only be applied to methods. • A native method will always have platform dependent code like C. • A native method should not have the implementation and should end with ‘;’. • A native methods implementation is
  20. 20. synchronized • synchronized can only be applied for methods. • Any method or block that is synchronized will only allow one single thread to execute the code at a given time. • synchronized can be used with any access
  21. 21. transient • only instance variables can be marked as transient. • A variable marked as transient will not be serialized. volatile • volatile can only be applied to instance
  22. 22. Access and non access modifiers - classes • public • default • abstract • strictfp •
  23. 23. Access and non access modifiers- variables and members Methods Instance variables Local variables public public - protected protected - default default - private private - static static - final final final strictfp - - native - - - transient - synchronized - - abstract -
  24. 24. Thank you Follow me on to get more updates on latest video posts Subscribe on Twitter : @java9s facebook: