Solved

final class

Posted on 2011-03-25
10
362 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:gudii9
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 35220312
it will output:

amethod

final has no impact on that example
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 125 total points
ID: 35221422
if you use final keyword infront of class means then that clas cant extends only    but you can create instances.....
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35222212
you can create instances only in that package not out side the package.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 19

Assisted Solution

by:Jim Cakalic
Jim Cakalic earned 250 total points
ID: 35223135
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
 
LVL 7

Author Comment

by:gudii9
ID: 35234525
>>final has no impact on that example

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

Expert Comment

by:Jim Cakalic
ID: 35234875
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
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35239138
>>>>>>>public void sayHello(final String name)

jim normally String is final class only
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35239226
>>>>>>>>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
 
LVL 19

Assisted Solution

by:Jim Cakalic
Jim Cakalic earned 250 total points
ID: 35242254
@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
 
LVL 7

Author Comment

by:gudii9
ID: 35380146
>>>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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question