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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:33:59+00:00 2026-05-11T14:33:59+00:00

See the two ways of class definition: //definition: 1 public class MyClass{ private MyObject

  • 0

See the two ways of class definition:

//definition: 1 public class MyClass{      private MyObject obj = new MyObject();     private String   str = 'hello world';      // constructor     public MyClass(){      } }  // definition: 2 public class MyClass{      private MyObject obj = null;     private String   str = null;      // constructor     public MyClass(){         obj = new MyClass();         str = 'HelloWorld';     }     } 

My question is: when are the class variables loaded. How are they loaded?

How does their initialization happen? If their initialization can happen as in definition 1, what is the purpose of a constructor?

Which is the preferred way of defining the class and why? Is the behavior same across C++/C#/Java or this behavior differs across them?

Any clarification comment on the above is welcome.

  • 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. 2026-05-11T14:34:00+00:00Added an answer on May 11, 2026 at 2:34 pm

    This depends on the language, but most languages initialize fields before calling the constructor. Generally I advocate doing things in context, initialization is usually only relevant where the fields are declared. However, as John pointed out, sometimes you need to do something which doesn’t make sense/is possible in one line.

    Also, as always, order is important if you have fields that depend on other fields for their initialization. In some languages, like ActionScript, this means the order of the declarations determine the order of initialization:

    Works:

    public class Foo {     private var bar:Array = [1, 2, 3];     private var baz:Array = bar.concat([4, 5, 6]); } 

    Doesn’t work:

    public class Foo {     private var baz:Array = bar.concat([4, 5, 6]);     private var bar:Array = [1, 2, 3]; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen two ways to implement the new method in a derived class. Method
Let's say I have the following class: class MyClass { private: int Data; public:
In a class inherited from dict, why don't the two ways of defining an
I see two general ways of handling instances of classes in PHP. Take, for
There are two ways to do this the way I see it without flash.
I can see two ways to hook up the ViewModel to the View. One
I see there are two ways to create update query in Hibernate. First you
I often see two conflicting strategies for method interfaces, loosely summarized as follows: //
I often see two styles, INSERT select and insert into select, what are the
When I compare two array values I see two strings that look the same.

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.