The Artima Developer Community
Sponsored Link

Java Answers Forum
Using Final in method parameters

6 replies on 1 page. Most recent reply: Dec 10, 2003 2:11 AM by Jonathon Brozny

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
sudheesh

Posts: 4
Nickname: sudhee
Registered: Dec, 2003

Using Final in method parameters Posted: Dec 9, 2003 2:47 AM
Reply to this message Reply
Advertisement
Hi ,

whats the exact difference between the follwing two methods

public void showValue(int i)

public void showValue(final int i)


Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: Using Final in method parameters Posted: Dec 9, 2003 7:24 AM
Reply to this message Reply
With (final int i) i cannot be reassigned. Its a constant.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Using Final in method parameters Posted: Dec 9, 2003 7:28 AM
Reply to this message Reply
Check the heading New Uses for final in this url.

http://java.sun.com/docs/books/tutorial/post1.0/converting/languageChanges.html

public class Util {
 
  public static void testing(final int x){
    x=25;
  }
  public static void testing1(int x){
    x=25;
  }
 
  public static void main(String[] args) {
    int x = 20;
    testing(x);
    testing1(x);
  }
}


in the method testing its illegal to change the value of x since its declared final, a final variable once assigned cannot be changed. And this will be flagged at compile time.

sudheesh

Posts: 4
Nickname: sudhee
Registered: Dec, 2003

Re: Using Final in method parameters Posted: Dec 9, 2003 11:00 PM
Reply to this message Reply
Thanx for the url.It was helpful.
but it says
"You can defer initialization of a final field or variable"
but is that possible?
The final variable needs to be initiated when it is declared.
For example this file wont compile.
public class Util {
final int a;
}
Can u pls give me an example of deferreed initialization?

Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: Using Final in method parameters Posted: Dec 10, 2003 12:22 AM
Reply to this message Reply
You have to initialize finals in the constructor. This give you a little more flexability because each object can have a different value for that field.

public class Util {
	final int a;
 
	Util (int somevalue){
		a = somevalue;
	}
}

sudheesh

Posts: 4
Nickname: sudhee
Registered: Dec, 2003

Re: Using Final in method parameters Posted: Dec 10, 2003 12:35 AM
Reply to this message Reply
Ok.That was heplful.
and just one more clarification needed.
For every method which has a parameter [which wont be changed anyway]if i use final keyword then that would really increase the performance a bit.Am i right?

Then y is it that we dont normally do it in most of the code?

Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: Using Final in method parameters Posted: Dec 10, 2003 2:11 AM
Reply to this message Reply
If it is a compile time constant then the compiler will make some optimizations.

I don't know that "we" don't use it "normally". I don't use it normally because I normally don't have a lot of constant values. But it does depend on what you are working on for how often you would use it I guess.

Flat View: This topic has 6 replies on 1 page
Topic: UnsupportedDataTypeException in mail sendprogram Previous Topic   Next Topic Topic: how to validate username and password in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use