Declaration and Access Control

1) Write code that declares constructs and initializes arrays of any base type using any of the permitted forms both for declaration and for initialization.
1.Declare a variable to hold the array.
2.Create a new array object and assign it to the array variable.
3.Store things in that array. The first way is to use the new operator to create a new instance of an array:
int[] a = new int[10];
variable len will have 5, as there are 5 elements in the names array.
System.out.println("Name: " + names[1]);

will print "Jo" and not "Harry"

System.out.println("Name: " + names[5]);

will give you a Runtime Exception called ArrayIndexOutOfBoundsException

names[1] = "Jack";

Remember: The length of the array is 5, but its subscript can only go up to 4. Arrays start numbering from 0. Subtract 1 from the length of the array to get its largest element.



2) Declare classes inner classes methods instance variables static variables and automatic (method local) variables making appropriate use of all permitted modifiers (such as public final static abstract and so forth). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

public :

private : protected :


default :

abstract :
eg,
public abstract class A {
public abstract void print(); //Note :No curley braces
//{} and no method body defined

public void test() {
//testing..........
}
}

class B extends A {
public void print() {
System.out.println("hi");
}
}

static :
public class test {
public static void method() {
this.print();
}
public static void print() {
System.out.println("Test");
}
public static void main(String arg[]) {
method();
}
}

The class fails to compile stating that the variable "this" is undefined.

eg,

class stat {
static int x = 0;
stat(){x++;}
}

stat s1 = new stat();
stat s2 = new stat();
s1.x=10;
s2.x=20;
y=s1.x;

y will have 20 and not 10 because x is static. So the better way to refer a static variable is via class name
stat.x=200;

way to access static field inside a static method is,

class stat {
static int x = 0;
public static void main(String s[]) {
System.out.println(x);
}
}

way to access non-static field inside a static method is,

class stat {
int x = 0;
public static void main(String s[]) {
stat s = new stat();
System.out.println(s.x);
}
}

final :

class subMath extends java.lang.Math {
}

You will get a compiler error saying "Can't subclass final classes" because java.lang.Math class is final.

class Test {
final void print() {}
}
class anotherTest extends Test {
void print() {} //final methods can't be overridden
}
eg for static and final variable :
class stat {
static int x=5;
int z = 5;
final int y = 6;
public static void main(String arg[]) {
stat s = new stat();
s.x=7;
s.z=9;
s.y=8; //can't assign a value to final variable
System.out.println(s.x); //prints 7
System.out.println(stat.x); //prints 7
System.out.println(stat.z); //can't make static
//reference to non-static variable
System.out.println(s.z); //prints 9

}
}



modifiers class methods variables
final Y Y Y
abstract Y Y N
static N Y Y
native N Y N
transient N N Y
volatile N N Y
synchronized N Y N

Remember :

Member Visibility
Accessible to public protected default private
same class Y Y Y Y
class in same package Y Y Y N
subclass in different package Y Y N N
Non-subclass in different package Y N N N



3) For a given class determine if a default constructor will be created and if so state the prototype of that constructor.

Constructors look a lot like regular methods, but some of them to be noted are :

class aClass{
String s;
void aClass() {
s="hello";
}
void print() {
System.out.println(s);
}
public static void main(String arg[]) {
aClass a = new aClass();
a.print();
}
}
output :
null

As there is a return type, "void aClass()" is considered as a method and not as a constructor. So, variable s is local to method aClass() and not accessible in method print()

Note: The defualt constructor is not inherited. It is created for you by the compiler if and only if, you don't provide any other constructors in the source of the particular class.

eg,

class Construct{
Construct(int i) {
intvalue=i;
}
public static void main(String str[]) {
Construct c = new Construct();
}
}

This gives error saying there is no matching constructor. if you didn't create any constructor explicitly, creating this instance with new won't give any error, because it takes the default constructor.



4) State the legal return types for any method given the declarations of all related methods in this or parent classes.

See objective 19




Copyright © 1999-2000, Jyothi Krishnan
All Rights Reserved.







Click Here!