FIELD DECLARATION IN JAVA

Field Declarations in Classes:

The purpose of a field is to hold a value inside an object or a class . A field must be declared in a class declaration. A field-declaration has one of the forms

field-modifiers type fieldnamel, fieldname2,...;
field-modifiers type fieldnamel = initializer1,...;

The field-modifiers may be a list of the modifiers static, final, transient and volatile and at most One of the access modifiers private, protected, and public .

If a field f in class C is declared static, then f is associated with the class C and can be referred to independently of any objects of class C. The field can be referred to as C.f or o.f, where o is an expression of type C, or, in the declaration of C, as f. If a field f in class C is not declared static, then f is associated with an object (also called instance) of class C, and every instance has its own instance of the field. The field can be referred to as o.f, where o is an expression of type C, or, in nonstatic code in the declaration of C, as f.

If a field f in class C is declared final, the field cannot be modified after initialization. If f has
reference type and points to an object or array, the object's fields or the array's elements may still be modified. The initialization must happen either in the declaration or in an initializer block ,
or (if the field is nonstatic) precisely once in every constructor in class C.A field initializer may be an expression or an array initializer . A static field initializer can refer only to static members of C and can throw no checked exceptions.

A field is given a default initial value depending on its type t. If t is a primitive type, the field is initialized to 0 (when t is byte, char, short, int, or long) or 0.0 (when t is float or double) or false (when t is boolean). If t is a reference type, the field is initialized to null.

Static fields are initialized when the class is loaded. First all static fields are given their default initial values, then the static initializer blocks static field initializers are executed, in order of
appearance in the class declaration.

Non static fields are initialized when a constructor is called, at which time all static fields have been initialized already .

If a class C declares a non static field f, and C is a subclass of a class B that has a non static field f, then every object of class C has two fields, both called f: one is the B-field f declared in the superclass B, and one is the C-field f declared in C itself. What field is referred to by a field access o.f is determined by the type of o .

The Member Access Modifiers :private, protected, public

The access modifiers private, protected, and public determine where else the member is accessible.If a member is declared private in top-level class C or a nested class within C, it is accessible in C and its nested classes, but not in their subclasses outside C nor in other classes.

If a member in class C is declared protected, it is accessible in all classes in the same package C and in subclasses of C, but not in non-subclasses in other packages. If a member in class C is not declared private, protected,or public, it has package access,or default access, and is accessible only in classes within the same package as C, not in classes in other packages.

If a member in class C is declared public, it is accessible in all classes, including classes in other packages. Thus, in order of increasing accessibility, we have private access, package (or default) access, protected access, and public access.

Private Member Accessibility:

A private member is accessible everywhere inside the enclosing top-level class (and only there).

class Access {
private static int x;
static class SI {
private static int y = x;
Access private x from enclosing class
}
static void m() {
int z = SI.y;
Access private y from nested class
}
}

Method Declarations:
A method must be declared inside a class. A method-declaration declaring method m has the form method-modifiers return-type m (formal-list) throws-clause
method-body
The formal-list is a comma-separated list of zero or more formal parameter declarations, of form
parameter-modifier type parameter-name

The parameter-modifier may be final, meaning that the parameter cannot be modified inside the method, or absent. The type is any type. The parameter-name is any name, but the parameter names must be distinct. A formal parameter is an initialized variable; its scope is the method -body.The method name m together with the list t1,..., tn of declared parameter types in the formal-list determine the method signature m(t1,..., tn). The return-type is not part of the method signature.


A class may declare more than one method with the same method-name, provided they have different method signatures. This is called overloading of the method-name. The method-body is a block-statement and thus may contain statements as well as declarations of variables and local classes. In particular, the method-body may contain return statements.

If the return-type is void, the method does not return a value, and no return in method-body can have an expression argument. If the return-type is not void but a type, the method must return a value: it must not be possible for execution to reach the end of method-body without executing a return statement. Moreover, every return statement must have an expression
argument whose type is a subtype of the return-type.

The method-modifiers may be abstract or a list of static, final, synchronized ,and at most one of the access modifiers private, protected, and public .If a method m in class C is declared static, then m is associated with the class C; it can be referred to without any object. The method may be called as C.m(...) or as o.m(...), where o is an expression whose type is a subtype of C, or, inside methods, constructors, field initializers, and initializer blocks in C, simply as m(...).

A static method can refer only to static fields and methods of the class.If a method m in class C is not declared static, then m is associated with an object (instance) of class C. Outside the class, the method must be called as o.m(...), where o is an object of class C or a subclass, or, inside non static methods, non static field initializers, and non static initializer blocks in C,simply as m(...). A non static method can refer to all fields and methods of class C, whether they are static or not.

If a method m in class C is declared final, it cannot be overridden (redefined) in subclasses.
If a method m in class C is declared abstract, class C must itself be abstract (and so cannot be
instantiated). An abstract method cannot be static, final, or synchronized, and its declaration
has this form, without a method body:

abstract method-modifiers return-type m(formal-list) throws-clause;

The throws-clause of a method or constructor has the form throws E1, E2, ...
where El, E2,... are the names of exception types covering all the checked exceptions that the method or constructor may throw. If execution may throw exception e, then e is either an unchecked exception or a checked exception whose class is a subtype of one of El, E2,....

Method Name Overloading and Signatures in java:

This class declares four overloaded methods m whose signatures m(int) and m(boolean) and m(int, double) and m(double, double). Some of the overloaded methods are static, others non static. The overloaded methods may have different return types, as shown here.

It would be legal to declare an additional method with signature m(double, int), but then the method call m(10, 20) would become ambiguous and illegal. Namely, there is no way to determine whether to call m(int, double) or m(double, int).

class Overloading {

double m(int i) {
return i;
}

boolean m(boolean b) {
return !b;
}

static double m(int x, double y) {
return x + y + 1;
}

static double m(double x, double y) {
return x + y + 3;
}

public static void main(String[] args) {
System.out.println(m(10, 20)); output is Prints: 31.0
System.out.println(m(10, 20.0)); output is Prints: 31.0
System.out.println(m(10.0, 20)); output is Prints: 33.0
System.out.println(m(10.0, 20.0)); output is Prints: 33.0
}
}

Method Overriding and Overloading in java:
The class C1 declares the overloaded method ml with signatures ml(double) and ml(int), and the
method m2 with signature m2(int). The subclass C2 hides C1's method ml(double) and overloads m2 by declaring an additional variant.

class Cl {
static void ml(double d) {
System.out.println("lld"); }

void ml(int i) {
System.out.println("lli"); }

void m2(int i) {
System.out.println("12i"); }
}

class C2 extends C1 {
static void ml(double d) {
System.out.println("21d"); }

void ml(int i) {
System.out.println("21i"); }

void m2(double d) {
System.out.println("22d"); }
}

int i = 17;
double d = 17.0;
C2 c2 = new C2(); Type C2, object class C2
C1 c1 = c2; Type C1, object class C1
c1.m1(i);
c2.m1(i);
c1.m1(d);
c2.m1(d);
c1.m2(i);
output isPrints: 21i ,21i, 11d, 21d, 12i.

RELATED POST

CONSTRUCTORS IN JAVA

No comments:

Post a Comment