OCJP(Oracle Certified Java Program)



Totel:15 Click: 1 2 3
scjp1.5 Interview Questions And Answers

Page 1

The syntax for instantiating a static nested class is as much like a normal nested class its a very little different from a normal inner class,, we can see this code : 

class BigOuter {
static class Nested { }
}
class Broom {
public static void main (String [] args) {
BigOuter.Nested n = new BigOuter.Nested(); //Use both class names
}
}

We know that static nested class is referred to as top-level nested classes, or static inner classes, but they really aren't inner classes at all.While an inner class enjoys that special ,a static nested class does not. 

It is simply a non interrelationship with the outer class,class scoped within another. So with static classes it's really more about name-space resolution than about an implicit relationship between the two classes,

class BigOuter {
static class Nested { }
}


class MyWonderfulClass {
void go() {
Bar b = new Bar();
b.doStuff(AckWeDon'tHaveAFoo!); // Don't try to compile this at home
}
}
interface Foo {
void foof();
}
class Bar {
void doStuff(Foo f) { }
}

Here both of flavour ahev a main and one and only one most important diffrece is that : flavor one creates an anonymous subclass of the specified class type, whereas flavor two creates an anonymous implementer of the specified interface type. see this example : 
interface Cookable {
public void cook();
}
class Food {
Cookable c = new Cookable() {
public void cook() {
System.out.println("anonymous cookable implementer");
}
};
}

firstly we should know this code:-
class Maggie {
public void pop() {
System.out.println("Maggie");
}
}
class Food {
Maggie m = new Maggie() {
public void mag() {
System.out.println("anonymous Maggie");
}
};
}


 Now we accusing about whats the happening in this code :
  1. here two classes are mentioned maggie and food.
  2. maggie has one method mag()
  3. Food has one instance variable, declared as type maggie.
  4. That's it for Food. Food has no methods.
           here most impotent thing is  :
Maggie reference variable refers not to an instance of Maggie, but to an instance of an anonymous subclass of Maggie.


Goto Page:

1 2 3