Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 26th, 2004, 08:48 AM
Registered User
 
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ravikumar681 Send a message via Yahoo to ravikumar681
Default Difference between static and final

<P>
hai ,

please tell me wat is the difference between static and final varibles
, static and final class.

thanks in advance for your replies
i need help under what situtation static is useful and final is useful.

<p>

 
Old June 2nd, 2004, 01:56 PM
Registered User
 
Join Date: Jun 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the ``final'' keyword, when applied to a variable, means the variable's value is constant.

e.g.
Code:
    final int RED = 1;
the variable RED cannot now be given another value. If you are familiar with C, it is analogous to the following:
Code:
    #define RED 1
The ``final'' keyword is useful when applied to variables whose values will not change during the lifetime of the program. If you've got constant values in your program which will not change throughout, it is useful to declare them as final.

The keyword ``static'' when applied to a variable OR a method means that the variable or method can be accessed without creating an instance of the class.

For examples of static variables see the Color class in the Java API. It uses static variables such as BLACK, BLUE, PINK etc.
They can be referenced like:
Code:
    useColor(Color.BLACK);
instead of
Code:
    Color myColorBlack = new Color();
    useColor(myColorBlack.BLACK);
static can be used for methods for much the same effect.


When ``final'' is applied to a class, the principle effect is that the class cannot be inherited from. For example, the following would throw an error:
Code:
   public final class Dog {
     public Dog() {
     }
   }

   public class JackRussell extends Dog {
      public JackRussell() {
      }
   }
For more information on the static keyword, see:
http://www.javacoffeebreak.com/faq/faq0010.html




Phil
 
Old June 24th, 2004, 07:54 AM
Registered User
 
Join Date: Jun 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

static means the reference is constant whereas final means the value is constant

Vijay





Similar Threads
Thread Thread Starter Forum Replies Last Post
Keyword final Leville Java Basics 4 September 19th, 2008 09:35 PM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
XML final output sanek55555 XSLT 8 November 2nd, 2006 04:12 AM
Blank Final Consigliori Java Basics 1 September 27th, 2006 09:25 AM
Blank Final Consigliori Java GUI 0 September 7th, 2006 01:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.