cbs-student
cbs-student

Reputation: 7

Can you explain, what the different is between theese to ways of instantiating an object?

Im looking at a template delivering from my computer science teacher, and i can see he instantiate a object an object of a class in two different ways.

The first way he does it is like this:

He starts off by declaring an object of the ProfileView-class

//Declare an object af Profileview

 private ProfileView profileView;

//Constructor of UserMainView

 public UserMainView() {
    initWidget(ourUiBinder.createAndBindUi(this));

    //Instantiate ProfileView objektet
    profileView = new ProfileView();

And here comes to second way he does it...

//OnModuleLoad - method 

public void onModuleLoad() {

   //Instantiate an object at ContentPanel "content"  / The screen
    ContentPanel content = new ContentPanel();

So the thing that im wondering about here is, what is the exact different between the two methods? And what is the exact different between only declaring an object, and instantiating an object? This is really imorptant to me because im gonna face an verbal exam in the end of this semester. Thanks in advance for help.

Upvotes: 1

Views: 112

Answers (3)

Ralf Kleberhoff
Ralf Kleberhoff

Reputation: 7290

Let's break your question down to the different elements.

Instance

Every time you execute one new Whatever(...) expression, you create exactly one new instance of exactly the Whatever class. This instance resides in a memory area called the heap. It survives there as long as you need it. It's always the same single instance no matter how often you store a reference to it into any field or variable.

new ProfileView()

creates one fresh ProfileView, no matter if you store that into a variable, a field, or just use it inside a more complex expression.

Field

A field is an element of an instance (or a class in case of a static field) that makes up its state. Declaring a field like in private ProfileView profileView; creates a place where you can store the reference to a ProfileView, and does not create any ProfileView instance. Initially, a field holds null. You can combine declaring a field with assigning an initial value, e.g. private ProfileView profileView = new ProfileView();, in this case a newly-created ProfileView instance.

Variable

A variable is a place where you can store a reference to an instance for usage inside one currently-executing method. Declaring a variable does not create any instance. The initial value of a variable is undefined, and the compiler signals an error if you try to read the value of an uninitialized variable.

The variable "begins to exist" when the program encounters its declaration, and "ceases to exist" at the end of the block (the closing curly brace) where it was declared. This doesn't tell anything about the existence of any instance that the variable might happen to refer to.

Upvotes: 1

forpas
forpas

Reputation: 164234

This is a declaration:

ContentPanel content;

After that the object content references nothing.
If you try to use this object before you initialize it the compiler will stop you
with a message like: "the variable content might not have been initialized".
You use this declaration just to notify the compiler of the type of object that the variable content will be.

You can later initialize it with:

content = new ContentPanel();

Now content references a newlly created ContentPanel object.

All this could be done in a single line:

ContentPanel content = new ContentPanel();

The above line of code is called instantiation of the Class ContentPanel
because it creates an instance (or an object) of that Class.

You can find more here: Creating Objects

Upvotes: 1

David
David

Reputation: 219127

There's no difference in how these objects are instantiated. Both use the new keyword to invoke a constructor, and both constructors are parameterless:

new ProfileView()

and:

new ContentPanel()

The difference is in the scope of the variables which hold the references to those objects. When you do this in a method or block of code:

ContentPanel content;

You are creating a new local variable called content of type ContentPanel within that method or block of code. That variable will only exist within that scope. As soon as the method or block of code concludes, the variable is gone. The object it references may outlive that scope, depending on how it's used. But the variable content exists only within that scope.

(Important note: Variables and the in-memory objects they reference are two distinctly different things.)

However, when you declare a variable at the class level:

private ProfileView profileView;

// methods here

Then that variable is now a class-level member. Any instance methods on that class can now access that same variable.

Upvotes: 2

Related Questions