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

  • Home
  • SEARCH
  • 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 6618583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:51:12+00:00 2026-05-25T20:51:12+00:00

In thinking in Java, page 566 gives the following example. class CountedInteger { private

  • 0

In thinking in Java, page 566 gives the following example.

class CountedInteger {
  private static long counter;
  private final long id = counter++;
  public String toString() { return Long.toString(id); }
}
public class FilledList<T> {
  private Class<T> type;

  public FilledList(Class<T> type) { this.type = type; }

  public List<T> create(int nElements) {
    List<T> result = new ArrayList<T>();
    try {
       for(int i = 0; i < nElements; i++)
       result.add(type.newInstance());
    } catch(Exception e) {
      throw new RuntimeException(e);
    }
    return result;
  }

   public static void main(String[] args) {
     FilledList<CountedInteger> fl = new FilledList<CountedInteger>(CountedInteger.class);
     System.out.println(fl.create(15));
   }
} 

I have three questions with respect to this example.

1) What is the usage of private Class type? Why is it private?

2) Why do the following, in particular ” this.type = type;”

public FilledList(Class<T> type) { this.type = type; }

3) The author claims:

Notice that this class must assume that any type that it works with
has a default constructor (one without arguments), and you’ll get an
exception if that isn’t the case.

I can not figure out how this statement was reflected in the above example. Thanks.

  • 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-05-25T20:51:12+00:00Added an answer on May 25, 2026 at 8:51 pm

    1) It doesn’t have to be, but that’s one of the things you do in Java–don’t expose things that don’t need to be exposed.

    2) That sets the property to the parameter of the constructor–that’s elemental Java.

    3) Because of the type.newInstance() call; without a default (no-arg) constructor it will fail.

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

Sidebar

Related Questions

In page 428 (the chapter about Type Information) of his Thinking In Java, 4th
I've been reading Bruce Eckel's Thinking In Java and in the chapter on generics,
I'm thinking about using the java application object to implement a simple cache, to
Does anyone know of a Java library that handles finding sentence boundaries? I'm thinking
I have a Java project that needs a addon interface. I was thinking about
Coming from a Java background, this is the way I'm thinking: The server provides
Coming from a Java background, this is the way I'm thinking: The server provides
I have a web app built on Java Servlet technology. I am thinking of
i am teaching myself java and i work through the exercises in Thinking in
I was thinking whether it is possible to bridge asp.net, php and java to

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.