Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

final class

Hi,

I read

http://www.jchq.net/certkey/0102certkey.htm
program

final class Base{

public void amethod(){
        System.out.println("amethod");
        }
}

public class Fin{
public static void main(String argv[]){
        Base b = new Base();
        b.amethod();
        }
}


 I was not clear on output. Whether Base class is final or not output did not change. How final effects output here. Any ideas, resources, links, sample code highly appreciated. thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can create instances only in that package not out side the package.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

>>final has no impact on that example

so this is seems to be irrelevant example for final then.
I read the original page and agree that this is a poor example for explaining the impact of final. The majority of examples I can think of would be negative - they won't compile because the point of final is to prevent introducing or changing some aspect of a design beit a constant value, a method parameter value, the definition of a method or the introduction of a subclass.

One interesting use of final is in the interaction between anonymous classes and variables defined in the definition context provided by the enclosing class. Say you had this interface:

public interface Speaker {
    public void speak(String speech);
}

And then used it in this implementation:

public class Primary {
    public void sayHello(String name) {
        new Speaker() {
            public void speak(String speech) {
                System.out.println("Hello, " + name);
            }
        };
    }
}

This example doesn't compile. As explained here: "An anonymous local class can use local variables because the compiler automatically gives the class a private instance field to hold a copy of each local variable the class uses. The compiler also adds hidden parameters to each constructor to initialize these automatically created private fields. Thus, a local class does not actually access local variables, but merely its own private copies of them. The only way this can work correctly is if the local variables are declared final, so that they are guaranteed not to change. With this guarantee in place, the local class is assured that its internal copies of the variables accurately reflect the actual local variables."

Adding the keyword final in front of the method variable allows the example to compile:
    public void sayHello(final String name)

Regards,
Jim
>>>>>>>public void sayHello(final String name)

jim normally String is final class only
>>>>>>>>final has no impact on that example

so this is seems to be irrelevant example for final then.

Best example is String thats why its immutable !!

http://download.oracle.com/javase/6/docs/api/java/lang/String.html 
public final class String
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

>>>putting final before the parameter declaration simply states that the parameter value supplied when calling the method cannot be modified in the method;


so what is the purpose of this program


>>public class Primary {
    public void sayHello(final String name) {
        new Speaker() {
            public void speak(String speech) {
                System.out.println("Hello, " + name);
            }
        };
    }
}

as you cannot really change name here.

Also this program is not dependiong on the interface. Even i remove interface it worked fine. please advise