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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:07:59+00:00 2026-05-23T18:07:59+00:00

I come from C++ background, where the field of a class are initialized inside

  • 0

I come from C++ background, where the field of a class are initialized inside the constructor. But here is java I see that I can initialize then while declaring, which is different from C++.

So, I my question was which is the best/traditional practice to initialize the field members of any class. Is it…

  • at the time to declaring?

    class testClass
    {
    private int x = 100;
    }

  • using non-static scope?

    class testClass{
    private int x;
    {
    x = 100;
    }
    }

  • or inside of a constructor?

    class testClass{
    private int x;
    testClass(){x = 100;}
    }

( Because I of my background I am a little biased towards initializing field members inside of a constructor.)

  • 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-23T18:08:00+00:00Added an answer on May 23, 2026 at 6:08 pm

    I don’t think there is a ‘best practice’ for initializing member variables. Obviously in Java you can initialize them in the declaration which is fine, or you can do it from the constructor. Unless you need to do something complicated (i.e. obtain a reference to a DataSource or something like that which might only be available with arguments passed in to the constructor).

    In other words, there isn’t a ‘right way’ of doing it, consistency is probably more important.

    That said, bear in mind that Java will initialise uninitialised member variables for you if you don’t do it. e.g. int variables will be set to 0, boolean variables will be set to false, any objects will be set to null, etc.

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

Sidebar

Related Questions

So I come from a background in Java where you can create a class
I come from Java Background and so used to Debugging using Eclipse but have
I come from a java background. But I would like a cross-platform perspective on
I come from Java background, where we have data structures with interfaces that if
I come from a Java background, where packages are used, not namespaces. I'm used
I come from a Java background and with any servlets-based technology, it's trivial to
I come from a C++ background where I can use template mixins to write
I come from a .NET background and will be going into the field of
I come from a background where I normally create one file per class. I
Sorry if the question is silly. I come from java background. In the following

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.