Home > JAVA > Class Members

Class Members

Class members means methods and instance (nonlocal) variable. If a class cannot be accessed, its members cannot be accessed.

 

Access Modifier

For class members, access means:

  1. Can access a member of anotehr class.
  2. Can inherit a member of it superclass.

Class members have four access control level:
public
Can be accessed by all other classes, even in other package.

private
Can be accessed only by code in the same class.
Not visible to sublcass, so private members cannot be inherited.
For best practice, instance variable must be mark by private. There can be access by methods (mark by public).

protected and default
Default members can be acces only by classes in the same package.
protected members can be accessed by other classes in the same package, plus subclasses regardless of package.
For subclasses outside the package, the protected member can be access only through inheritance.
A protected member inherited by subclass from another package is not accessible to any other class in the subclass package, except for the subclass own subclasses.
Local variables declaration cannot have access modifier. The only one modifier is final. They don’t get default value, you must initialized before use.

 

Nonaccess member modifier (method)

final
Final method cannot be overrided in a subclass.

abstract
An abstract method is a method that’s been declared (as abstract) but not implemented.
If a single method is mark by abstarct, the whole class must be declared abstract.
The firts nonabstract (concrete) class to extend the abstract class must implement all of the abstract class abstract method.

synchronized
The synchronized method indicates that a method can be accessed by only one thread at the time.

native
The native modifier indicates that a method is implemented in platform-dependent code, often in C.

strictfp
Force floating points (and any floating points operations) to adhere to the IEEE 754 standard. With strictfp, you can predict how your foalting points will behave regardless of the underlying platform the JVM is running on.

 

Nonaccess member modifier (variables)

final
This keyword makes it imposible to reinitialize that variable once it has been initialized with an explicit value.

transient
Tell the JVM to skip this variable when you attempt to serialize the object containing it.

volatile
Can be apply only to instance variable.
Tell the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master on the memory.

Categories: JAVA
  1. No comments yet.
  1. July 1, 2014 at 2:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: