Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: static final object

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb static final object

    Hi !!!

    I am little bit confused with static final object....i have done a sample pgm,i jst intialized
    one final static obj,

    after that i called ths static obj,i added two elements.and in my main method(),again i called up
    same static object and again i added one element and then i printed its jst printing .

    final keyword should not allow to override add elements correct?.can u explain me properly?..

    ths is sample code what i created........


    import java.util.*;
    class sortedSet 
    {
    	static final List list = new ArrayList();
    	void finalMethod(){
    		list.add("Test");
    		list.add("Test");
    	}
    	public static void main(String[] args) 
    	{
    		sortedSet set = new sortedSet();
    		set.finalMethod();
    		sortedSet.list.add("Sample");
    		System.out.println("sortedSet.list value !"+sortedSet.list);
    	}
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: static final object

    Final will prevent you from creating a new instance.

    For example, this isn't possible because of final.

    final List a = new ArrayList<Integer>();
    			a = new ArrayList<String>();

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: static final object

    Exactly! The final keyword will still let you change the properties or attributes of the mutable object so you can still call .add on a static list. You cannot however assign a new list to the final variable because once you've set it you cant re-set it.

    // Json

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: static final object

    Quote Originally Posted by kalees View Post
    final keyword should not allow to override add elements correct?.can u explain me properly?
    'Override' refers to declaring, when you write a subclass, methods that have the same signature (name, parameters) as methods in the superclass, so when called on a subclass instance, the subclass methods will be called instead of the superclass methods - they 'override' the superclass methods.

    Calling a method on an object several times is not overriding, it's just calling a method.

Similar Threads

  1. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 01:56 PM
  2. quite small (make the changelog non-static, i.e. load its content from a file)
    By Abdallah in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 30th, 2009, 09:00 PM
  3. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 11:10 AM
  4. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 09:19 AM
  5. Java program to display single and plural words
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 11th, 2009, 12:09 AM