Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:48:34+00:00 2026-06-14T21:48:34+00:00

I learn java from book Java. How to Program P. & H. Deitel On

  • 0

I learn java from book “Java. How to Program” P. & H. Deitel
On page 216 there is an example which used final in one of variable

private static final Random randomNumbers = new Random();

As far as I understand declaration final in variable mean, that variable is kind of constant, that mean when is initialized you cant change it anymore.
But above object(variable) is used in program twice to return random number

int die1 = 1 + randomNumbers.nextInt( 6 );
int die2 = 1 + randomNumbers.nextInt( 6 );

and it returns 2 different (random) values.
I think I lost something here. Program works good, but I dont understand what was purpose to use final in object declaration?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:48:35+00:00Added an answer on June 14, 2026 at 9:48 pm

    final means that the variable cannot change its value here – and indeed it can’t and doesn’t.

    Here the value of the randomNumbers variable is a reference to an instance of Random. It refers to the same instance, even though that instance produces (potentially) different numbers each time you call nextInt.

    It’s important to differentiate between a variable not changing value, and the object it refers to not changing internal state. As another example, you could have something like this:

    public class Person {
        private final List<Person> friends = new ArrayList<Person>();
    
        public void addFriend(Person friend) {
            friends.add(friend);
        }
    
        ...
    }
    

    Here we only have one list – you can never change friends to refer to a different object – but we can still mutate the list that the variable refers to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn java from bottom up, and I got this great book
I'm trying to learn Java from this book The Art and Science of Java
I am coming from Java and try to learn Javascript right now. Doing that
I'm trying to learn web development in Java and, from what I have seen
I'm after a book to learn about the internals of the Java virtual machine.
I'm reading a book called Java and C: Key Differences, Learn Objective-C for Java
I am trying to learn Java by doing some assignments from a Stanford class
I'm moving from Java to C++ and have really enjoyed it. One thing I
I'm coming from a .NET background and trying to learn Java. I keep seeing
I'm trying to build a web page to better learn Java and SQL. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.