Difference between an Instance and Object in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In Java, what is the difference between instance of a class and Object of a class.
For a class A, Take a look :
line 1 : A a; // Declaring a reference variable of class A
Line 2 : a = new A();// Instantiating an object of class A
So....can the line 2 also be : // Instantiating an instance of class A
which mean Instance and Object are absolutely the same thing ?
Please give an objective answer than subjective.

Line 1 declares a variable, it doesn't reference anything, though, its value is null.
Line 2 creates a new object and assigns it to the variable a.
An object is an instance of a class. A class is something used to create objects, an object is something created (instantiated) using that class as a template.
"instance" means a specific occurrence of something. For instance, you could talk about database instances, where each instance is an installation in a specific place on a specific server somewhere. Likewise with objects, an instance is a specific member of a category.

The term "instance of" is used to show you from which classes the specific object origins from. The objects Buddy, Lucky and Sparky are instances of dogs but also instances of animals. So the object itself can be an instance of multiple classes. A cat is also an instance of animals and an instance of cats but not an instance of dogs.
public abstract class Animals{...}
public class Dogs extends Animals{...}
public class Cats extends Animals{...}

The object and the instance are of different stories. It's like asking what is the difference between a Car and an Engine.
Anyway, an object is a representation of a class. A class is the file that you write and save.
Once you use a class and put it into memory, an object is created.
Instantiating is the process of using a class to create an object based on that class and putting it into memory.
A a; // Declaring a reference variable of class A
This means you are reserving a variable for a particular object or particular class.
a = new A();// Instantiating an object of class A
This means that you are using the A.class as your base class to create the object a. And this process is basically Instantiation.

Line 1, you declare an variable which type is A, but the variable doesn't have any value, its value is null.
In Line2, you created an object of Class A using new, and assign a reference of that object to variable a, in another words, you assigned a reference of A class's object to the variable a.
a is called the instance of class A, the object it refers to is called the object of A

Related

Passing as a parameter or creating an instance. Which is a good programming practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have class A from which I need to pass an Object obj to function func2 in class B. The obj is required in func2 of class B.
class A {
Object obj;
.
.
public void func1() {
B b = new B(obj);
b.func2();
}
}
or
class A {
Object obj;
.
.
public void func1() {
B b = new B();
b.func2(obj);
}
}
I have usage of obj in class B only in func2.
Which is good programming practice either to pass the obj as parameter to func2 or create an instance of Object in class B and initialize Object in the constructor of class B. Or it doesn't make difference using either methods?
Appreciate any inputs on this.
UPDATE
To clarify my use case, Class A configures HttpClient and passes the instance of HttpClient to Class B. func2 in Class B executes the HttpRequest and returns the response. Is there a need to maintain HttpClient as instance variable in Class B or just pass it as parameter to func2. Yes, I have to create a new Instance of B everytime in func1 according to the design.
Constructors are used to initialize the fields of a class, so if you are passing obj to the constructor of class B, then obj should be a field inside class B i.e., obj maintains HAS-A relationship with B.
The other case you mentioned is that when you pass obj as a method (like func2) argument, which you typically use it inside func2 computations.
So, you need to check whether obj makes sense to be as a field of class B or not so that it can be initialized as part of the constructor. So, it all depends upon the design of class B.
Also, to answer to your question is, just think about the relationship between obj and class B in the object oriented world,more importantly, if you take classes like A, B, obj, etc.. you can't apply/understand the object oriented concepts easily. Rather, consider the real world classes and objects like class BankAccount or class Product or class Machine, etc..
Since you only use obj in func2 I would suggest passing it to the method as a parameter. This will allow the object to be garbage collected sooner instead of waiting for B to be destroyed. However, if the method is frequently used then it may decrease performance. It is impossible to tell with the information provided.
You can use bench-marking to test the performance and compare the two approaches in your project.

When creating a reference variable, what are we exactly doing? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Noob here, with some questions about fundamental things in Java.
If we have a class Person with a constructor (also Person) we can create a reference variable like this:
Person johnny = new Person();
Let's say we have just created in a class People an object, johnny.
It holds an "address" to where the object and its values are kept, unlike objects of primitives types do (e.g. int x = 5; has the value 5, johnny is a "link" to the value - if I've understood things correctly.)
1st word: Person is a class/type (<-- edited here), but:
What does a class/type do? What does it entail? Perhaps: what does a type do, if it is not a 'primitive type'?
2nd word: johnny is a reference variable, of type Person.
- If I am missing something, please enlighten me :)
3rd word: new this just states that we are creating the new object.
4th word: second Person:
This is the object we are creating?
Is this the constructor or the class we are referring to?
ALSO:
The value (address) that the variable is referring to is everything after the equal sign? Is the address new Person?
LASTLY: Polymorphism.
So if we have a sub-class Adult, which extends the super-class Person, then we can do this:
Person amanda = new Adult();
What are we doing now?
Is amanda of class type Person and refers to an object in the class Adult?
Thanks for your help :)
1st word: 'Person' is a class type
Yes, but it might just as well be a primitive type, does not matter for that "first word" which is the type declaration for the variable.
Another example would be
int a = 1;
When you declare a variable or parameter in Java, the name is preceded by the type.
2nd word: 'johnny' is a reference variable, of type Person
That's right. You name your variable johnny here. And its type is what you specified for it right in front of the name.
3rd word: 'new' this just states that we are creating the new object.
new means that we are calling a constructor (of the class that is following the new). That is indeed how you create objects in Java.
4th word: second 'Person
That is the name of the class that you are calling the constructor of. This will always return a new instance of exactly that class (not of a subclass). There can be more than one constructor to choose from for a given class, they are differentiated by parameter counts and types (in your case, a constructor without any parameters).
The value (address) that the variable is referring to is everything after the equal sign? The address is 'new Person'?
new Person() returns a reference to the newly created object instance. It will be stored in your variable johnny.
LASTLY: Polymorphism.
Your Adult class is a subclass of Person. This means that you can use it in all situations where a Person would be required. In particular, you can assign it to a Person-type variable (such as johnny) and call all Person methods on it.
This is all too confused for words.
When creating a reference type, what are we exactly doing?
Nothing. There is no such operation in Java.
When we have a class (or super-class, I'll get back to that) Person with a constructor (also Person) we can create a reference type like this:
No you can't. What you are creating here is a reference. You are not creating a type, let alone a 'reference type'.
Person johnny = new Person();
We have just created in a new class People (e.g.) an object, johnny.
I see no evidence of a class People, but I'll take your word for it.
It holds an "address" to where the object and its values are kept, unlike objects of primitives types do (e.g. int x = 5; has the value 5, johnny is a "link" to the value - if I've understood things correctly.)
Correct.
1st word: 'Person' is a class type, but:
No it isn't. Person is a class. Period.
What does a class type do? What does it entail?
Nothing because it doesn't exist. A class on the other hand is a kind of template for constructing objects from, that determines its data members and what code can be called on it.
Perhaps: what does a type do, if it is not a 'primitive type'?
It extends the type system beyond the primitive types, in a way which can be reasoned about and computed, via inheritance, interface implementation, and the Liskov substituion principle (q.v.).
2nd word: 'johnny' is a reference variable, of type Person. - If I am missing something, please enlighten me :)
Correct.
3rd word: 'new' this just states that we are creating the new object.
Correct.
4th word: second Person: This is the object we are creating?
This is the class (or 'type' if you insist) of the object we are creating.
Is this the constructor or the class we are referring to?
Both. The arguments or lack of them determine which constructor is called. The name of the constructor is the same as the name of the class, by the rules of Java.
The value (address) that the variable is referring to is everything after the equal sign? The address is 'new Person'?
The variable refers to the object that was created. There is no concept of 'address' in Java.
LASTLY: Polymorphism. So if we have a sub-classes 'Adult', which extends the super-class 'Person', then we can do this:
Person amanda = new Adult();
What are we doing now?
You are creating an object of type Adult and referring to it via a reference of type Person, which can only succeed if Person is a base class of or an interface implemented by Adult.
Is 'amanda' of class type Person
'amanda' is a reference of type/class Person.
and an object in the class 'Adult'?
amanda isn't an object at all. The object it refers to is an object of class Adult.

Java: new, inheritance and objects number

I am trying to understand how inheritance is realized inside JVM. It seems to me, that if we have the following code:
class A {
int aa;
}
class B extends A{
int bb;
}
....
B b=new B();
Inside the JVM three objects will be created:
object of B (with field int bb),
object of A (with field int aa)
object of Object.
Of course the programmers see only one object of class B. Am I right? Or is only one object created inside JVM?
What I think:
The new returns the reference to B. Why I think so is (for example) that if we override some method from A in B we can always get it using super. Besides in default constructor B the first line will be call to default constructor A in which we can call the constructor on certain object ONLY IF this object exists. Therefore a separate A object exists?
At first, the spec says that the internal structure of objects is not specified, so in theory, a JVM could internally create more than one object, where B contains only fields new to B, and a link to an A object, which contains the fields of A.
It also says something about the Oracle JVM implementation: A class instance contains three pointers. One to a method table, one to some space in heap where the data of the instances fields is, and one the Class object that instance belongs to.
You can conclude from that, that there is only one instance per object created, namely the instance of B. The method table for this instance contains all methods from B, A and Object, as well as the heap space contains all data from fields from B, A (and Object).
Here only one object will create. it is B object. But B object has state and behaviors of A object. As super type of every class is Object class, B object has Object's state and behavior too. Thanks
Classes represent a type of objects, but only instances create real objects. So only one object is created and it corresponds the result merge of class B with class A.

why Reference Objects hold the references of a Particular Type only [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
when we allocate an object on heap and store its reference using the 'new' keyword in the reference objects like this
Class referenceObject = new Class();
every thing work fine
but why cannot our 'refernceObject' hold the reference to the another type like:
referenceObject = new AnotherClass();
as the references are the memory addresses pointing to the memory location .
i mean if i have an integer type i can assign any Integral value (please avoid sizes)
let us say 1 to 10 i.e
int i=1;
i=2;
...
i=10;
this works as we can say the type we assigning is same int for this example
what confuses me is why is not the same true for references .
more examples
i think it is like :
we can assign any value which is the element of Set(Integers) to 'i',
but we cannot assign any value which is the element of Set(References) to 'objectReference'
if it is like this what is the reason ?
any suggestion/help is welcomed and appretiated
To Downvoters: please comment your reason for downvoting or is it like you can't answer this
Java is a strongly typed languages. Therefore, when you declare a variable of type SomeClass, it can only refer to instances of SomeClass or sub-classes of SomeClass. Based on the fact that the variable has a specific type, the compiler allows you to call only methods declared for that type (or super-classes of that type).
Let's say you have 2 classes. Bird and Human.
When you declare a variable Bird tweety; You say that tweety is a bird and it must hold a Bird object.
If you assign tweety = new Human(); you will get a compile time error because tweety is a Bird and can't hold a Human object.

Instance vs Object : The unclear debate [duplicate]

This question already has answers here:
Difference between object and instance
(15 answers)
What exactly is an instance in Java?
(12 answers)
Closed 8 years ago.
In Java, what is the difference between instance of a class and Object of a class.
For a class A, Take a look :
line 1 : A a; // Declaring a reference variable of class A
Line 2 : a = new A();// Instantiating an object of class A ( An object/instance is created
on Right hand side of the equation)
So....can the line 2 also be : // Instantiating an instance of class A
which mean Instance and Object are absolutely the same thing ?
Please give an objective answer than subjective.
.So an instance and object is same ? No difference at all. An object is an instance of the class .... or an Instance is an object of a class....both are same ?
For all intents and purposes, object and instance can be used interchangeably, but the accepted answer at this link will give you a good understanding of how you should use the two:
Difference between object and instance
Yes, I'd agree that "instance" and Object are the same thing.
1 : A a; // Declaring a reference variable of class A
2 : a = new A();// Instantiating an object of class A
3 : Object o = a; // a is also an object
All instances in Java are also Objects, so they're the same thing. That's an is-a relationship. You can say that any instance in Java is-a Object. Objects are a type (class), and you can make instances of just type Object alone if you want.
Object x = new Object();
Classes are also objects.
4 : Class<A> atype = a.getClass();
5 : Object otype = atype;
So objects (instances) have-a Class, and classes are objects too. I think this is why things are so murky, all these words bear a very close relationship. Note all the things on the left hand side are also called reference types.

Categories