Professional Documents
Culture Documents
Note:
Default access modifier is special because it doesn’t have an associated
keyword. When no explicit access modifier is specified, the types or
members have default accessibility.
public:
1) This is the least restrictive access modifier which means the widest range
of accessibility, or visibility.
2) When applied to a class, the class is accessible from any classes
regardless of packages.
default:
1) When a class, variable or method has default accessibility, it is accessible
to only classes in the same package.
1
private:
1) Members marked private can't be accessed by code in any class other
than the class in which the private member was declared.
protected:
1) Protected members of a class are visible within the package but they can
be inherited to sub classes outside the package.
final:
1) When used in a class declaration, the final keyword means the class can't
be subclassed.
static:
1) The static modifier is used to create variables and methods that will exist
independently of any instances created for the class.
2) All static members exist before you ever make a new instance of a class,
and there will be only one copy of a static member regardless of the
number of instances of that class.
abstract:
1) An abstract class can never be instantiated.
synchronized:
1) When a method is synchronized it can be accessed by only one thread at
a time.
transient:
1) If you mark an instance variable as transient, you're telling the JVM to
skip (ignore) this variable when you attempt to serialize the object
containing it.
volatile:
1) The volatile modifier tells the JVM that a thread accessing the variable
must always settle its own private copy of the variable with the master
copy in memory.
2) The volatile modifier may also be applied to project managers
2
Q) What is the problem with the below code?
package org.nareshit.accessModifer.protected;
Answer: Invalid package name. protected is a java keyword.
Q) Can an inner class be declared private?
Answer: Yes. The inner class can be private.
Q) Explain access levels of differ access modifiers?
Answer:
Access Levels
Modifier Class Package Subclass (different package) World
Public Y Y Y Y
Protected Y Y Y N
no modifier Y Y N N
Private Y N N N
3
Q) What are the constraints on a static method or a static block of code?
A static method should not refer to instance variables without an instance.
Cannot use this operator to refer the current instance.
Q) Can we declare static members in a method?
Methods will have only local variables.
Static variables are class level and cannot reside inside a method.