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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:24:51+00:00 2026-05-24T08:24:51+00:00

What is the Difference in between 2 statements int main() { A a =

  • 0

What is the Difference in between 2 statements

int main() 
{ 
    A a = new A();
    A a;
}

Please explain this two object creation statements.

  • 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-24T08:24:53+00:00Added an answer on May 24, 2026 at 8:24 am

    The first command allocates a variable on the stack (A a), and initializes on the heap (new A()).

    The second one only allocates the variable on the stack. It is not initialized, and therefore cannot be used until you assign it, either by a return value from a function or calling the class constructor.


    Side Note: When your program is compiled and being run, it doesn’t even remotely resemble the code you wrote. Variables are loaded right before you need them. The code you have above would roughly look like this:

    .method private hidebysig static void  Main(string[] args) cil managed
    {
      .entrypoint
      // Code size       8 (0x8)
      .maxstack  1
      .locals init ([0] class DERP.Program/A a,   // This code here declares a local
                                                  // variable: A a;
               [1] class DERP.Program/A b)        // another local variable: A b;
       nop
       newobj     instance void DERP.Program/A::.ctor()  // This is: new A()
       stloc.1                           // this loads the new A() we created
                                         // into A b;  (stloc.1 means to store
                                         // the last item we created into the
                                         // local variable at index [1]
       ret
    } // end of method Program::Main
    

    It’s okay if you don’t completely understand what each of those commands are, but I’ve commented it to try and make it as straight forwards as possibly. Since we never assign A a to anything, it just sits on the local stack, hanging out, doing nothing. We can’t use it because it doesn’t point to any object.


    There are many different reason in code you might see the declaration separated from the actual assignment.

    For example, sometimes you need to declare a variable outside of a try {} catch {} clause. Lets say your class takes a value in its constructor. You have a function that gets that data, say from a database. However, since its a DB call, you want to catch the exception, and if its thrown, initialize the class with a default value, instead of the returned value from the DB call.

    Due to the way scoping works in C#, variables declared inside a try {} catch {} are not accessible outside of it, hence you would need to declare the variable earlier in code before you initialize it.

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

Sidebar

Related Questions

I have been trying to explain the difference between switch statements and pattern matching(F#)
In Access, what is the difference between these two statements? DBEngine.BeginTrans and DBEngine.Workspaces(0).BeginTrans The
Is there any difference between these two LINQ statements: var query = from a
What is the difference between the following two statements? alter table [dbo].[Demo] add constraint
Is there a difference between using exit() or just return statements in main() ?
can somebody please tell me the difference between the following two code snippets: //Code
What is the difference between these three statements? static const int foo = 42;
What's the difference between these two statements? I use them in my rails app
I have a class named A . What the difference between these two statements?
what is the difference between the below two statements? which one is more efficient?

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.