Learning by developing

...because practice teaches you

Package:Group of classes. First statement in the source files “package com.abc” before the imports.

Nested/Inner class:Class defined inside another class. They can be non-static, method-local, anonymous or static. Two inner classes can extend from each other by using their “names” without anything else(no outer class´s suffix is needed).

It should be accessed like:

1st. From inside the internal class: myInner i = new myInner().

2nd. From OUTSIDE the internal class(myOuter):

Access through outer class myOuter.myInner.myInnerMethod().

3rd. Instanciate object from internal class – we need a reference to the external class:

MyOuter mo = new MyOuter();

MyOuter.MyInner mi = mo.new MyInner();

An Inner class can access the outer class´s members(even private and static members) through:

OuterClassName.this().method(); //(or variable)

Annonimous class: A a = new A(){…}; ← Needs to finish with a semicolon(;). Note: even if A is an interface and we call ‘A a’, as this is an anonnimous class, is NOT the same as the interfaceBUT if a class exists with the same name(A) IT EXTENDS IT.

Method local inner classes: Can be instantiated in the method where it is defined and can be also in a constructor, local block, a static initializer or an instance initializer. It cannot be public(local variables cannot be public) or access the local variables of the enclosing method unless they are final. The only modifiers for such a class are abstract or final:

E.g.: button1.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ …} } );

Note: Method local inner classes can access to its outer class variables but NOT to the method variables UNLESS they are marked as FINAL

Non-static inner classes: They do not use “static” keywords.

E.g.: class myClass{ class MyInner{}}. They allow modifiers: final, abstract, public, private and protected.

A non-static inner class HAS access to all the members of the outer class. It needs an instance of the outer class to be able to instanciate:

MyClass myClass = new MyClass();

myClass.MyInner myinner = myClass.new MyInner();

From inside the outer instance code, use the inner class name alone to instanciate it: E.g.: MyInner myInner = new MyInner();

Static nested classes: Static member of the enclosing class. It does NOT have access to the instance variables and method of the class. It does not need an instance of the outer class for instantiation. Note: it can have non-static members. E.g.: class Myouter{ static class MyNested{}} . So to instanciate it we would:

ClassA.InnerA ia= new ClassA.InnerA();


Object A = bew ClassB();

A.memberOfClassB ← gives error if “A” does not have “memberOfClassB”, even if “B” has it.

Inner Class of A:

class C{}//If there wasn´t any inner class, it would be chossen.

class A{

class C {…}// This one is used by methodA, since it searches for class C inside A and this is found.

// The method class created inside does not exist when instanciating C, else it would be chosen.

// If there were no inner or method classes, the outer class C would be chosen.

methodA() {

C x = new C();

class C {…}



class A{


public void m1(){…}

public void m2(){…}


class B{


A a = new A(){public void m1(){…}}; // extends from outer class A but overrides method m1()



Categories: Java

2 Responses so far.

  1. looky says:

    Is it possible to copy and paste news articles for my blog or is that copyright infringement?

Leave a Reply