1. 程式人生 > >java modefier修飾符

java modefier修飾符

  1. java 中的modefier(felt like that the modefiers are everywhere in java program)
  1. a java access modefiers specifies which classes can access a given class and its fields.

fields are the variables inside the class, the name ,position,salary,hireDate are the fields of the class Employee.

Accesee modefiers can be specified separately for a class, its constructors, fields and methods.

  •      private
  • default (package)
  • protected
  • public

  1. private access modifier

if a method or variable is marked as private ,then only code inside the same class can access the variable,or call the method.Code inside the subclass cannot access the variable or method,not can code from any external class.

Classes cannot be marked with the private access modifier. Marking a class with the private access modifier would mean that no other class could access it, which means that you could not really use the class at all.

Fields are often declared private to control the access to them from the outside world. In some cases the fields are truly private, meaning they are only used internally in the class. In other cases the fields can be accessed via accessor methods (e.g. getters and setters). Here is an accessor method example: