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 6740811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:39:03+00:00 2026-05-26T11:39:03+00:00

import java.util.*; public Class C { final Vector v; C() { v=new Vector(); }

  • 0
import java.util.*;

public Class C
{
     final Vector v;
     C()
     {
          v=new Vector();
     }

     C(int i)
     {
          //Here, it is an error. v might not have been initialized.
     }

     public void someMethod()
     {
           System.out.println(v.isEmpty());
     }

     public static void main(String[] args)
     {
          C c=new C();
          c.someMethod();
     }
} 

The Above code is a compile time error. I know but it says( in NetBeans) variable v should be initialized. When I initialized it in the overloaded constructor it fixes the problem and prints “true”. My problem is why should I initialize it again in the overloaded version of constructors.(I have initialized it once in the default constructor ) and I’m not even using the overloaded version. Why?

  • 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-26T11:39:03+00:00Added an answer on May 26, 2026 at 11:39 am

    When you create a new object, only one of the constructors of your class is called to initialize the object. You seem to think that all of the constructors are called, or that the default (no-arguments) constructor is always called. That is not the case.

    So, each constructor needs to initialize all the final member variables.

    Note that from one constructor you can explicitly call another one using this(...). For example, as the first line of your C(int i) constructor, you could add a line: this(); to call the C() constructor. Another solution is to initialize the member variable at the line where you declare it:

    public class C {
        // v will be initialized, no matter which constructor will be used
        final Vector v = new Vector();
    
        C() {
        }
    
        C(int i) {
            // ...
        }
    
        // ... etc.
    }
    

    Note that you don’t need to explicitly initialize non-final member variables; if you don’t initialize those, Java will initialize them with a default value (which is null for non-primitive type variables). However, you do need to explicitly initialize final member variables.

    Another note: Vector is a legacy collection class. You should prefer using ArrayList instead.

    Third note: Use generics to make your code more type-safe. For example, if you need to store strings in a list, use ArrayList<String> instead of the raw type ArrayList.

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

Sidebar

Related Questions

I have the following code: import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.Locale; public final class
import java.math.BigInteger; import java.util.concurrent.*; public class MultiThreadedFib { private ExecutorService executorService; public MultiThreadedFib(final int
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have a class like this : import java.io.*; import java.util.*; public class Contact_Info_Entry
import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; public class Main implements Runnable { private final CountDownLatch cdl1
import java.io.UnsupportedEncodingException; import java.util.Locale; public final class ForeignTextDemo { public static void main(String[] args)
import static com.crawler.constants.CrawlerConstants; import static com.crawler.constants.CrawlerConstants.CRAWLER4J; import java.util.Properties; public final class Configurations { private
Here's Pair.java import java.lang.*; import java.util.*; public class Pair<TYPEA, TYPEB> implements Comparable< Pair<TYPEA, TYPEB>
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]

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.