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.
LVL 7
gudii9Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
objectsConnect With a Mentor Commented:
it will output:

amethod

final has no impact on that example
0
 
Sathish David Kumar NConnect With a Mentor ArchitectCommented:
if you use final keyword infront of class means then that clas cant extends only    but you can create instances.....
0
 
Sathish David Kumar NArchitectCommented:
you can create instances only in that package not out side the package.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Jim CakalicConnect With a Mentor Senior Developer/ArchitectCommented:
The final keyword is way of saying "this thing can't change". When it is applied to a class it means the class can't have any subclasses, it cannot be extended. When applied to method it means that the method cannot be overridden by a subclass. And when applied to a variable it means that the value of the variable once defined cannot be changed.

Read here for more details and how/why final might be used in your designs:
http://www.codeguru.com/java/tij/tij0071.shtml

Regards,
Jim
0
 
gudii9Author Commented:
>>final has no impact on that example

so this is seems to be irrelevant example for final then.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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
0
 
Sathish David Kumar NArchitectCommented:
>>>>>>>public void sayHello(final String name)

jim normally String is final class only
0
 
Sathish David Kumar NArchitectCommented:
>>>>>>>>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
0
 
Jim CakalicConnect With a Mentor Senior Developer/ArchitectCommented:
@davidnsr: in the example I posted putting final before the parameter declaration simply states that the parameter value supplied when calling the method cannot be modified in the method; it says nothing about the class String or whether that class may be extended. The parameter could have been a primitive and 'final' would have meant the same thing.

Also, a final class and objects of a class being immutable are two completely different things. It is true that the String class is declared final so it cannot be extended. It is also true that objects of type String are immutable as they cannot be altered once constructed. But the two are not linked. I could declare a final class that was not immutable and a class whose objects were immutable but the class itself could be extended. The concerns of immutability and extensibility are orthogonal.
0
 
gudii9Author Commented:
>>>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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.