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.
Related
Let's say I have a class named Class and I created a new null object:
Class object = null
The constructor in Class is:
private int a;
private String b;
public Class() {
a = 144;
b = "Test";
c = null;
}
Will a, b, and c be equal to null?
If not, is there a way for me to create the object so that all three instance variables are null?
No. They won't be equal to anything because they won't exist. Nothing ever created an instance of Class so there is no instance.
As an analogy you're basically asking, "If I don't build a house, will that house's windows be open or closed?" They will be neither.
When you do this:
Class object = null;
What you have is a variable which can (at a later time) refer to any instance of type Class, but which currently refers to no instance.
is there a way for me to create the object so that all three instance variables are null
Kind of. You can add a constructor which doesn't set those values:
public Class () { }
And you can create an instance of that class via that constructor:
Class object = new Class();
Then you can observe your instance in the debugger to see what those values are. I'm not 100% sure in Java, but it's possible that an int can't be null. Which would make that part of the question kind of moot. (I know it can't in C#, but if it can in Java then ignore this part and carry on.)
An unassigned int local variable would be a compiler error if you try to use it. But this is a class field, not a local variable. In this case it's going to be automatically given its default value, which is generally null for reference types but 0 for primitive numeric types.
Let's say I have a class named Class
Given that java.lang.Class already exists, let's not. Let's say you have a class named MyClass.
and I created a new null object:
That's an oxymoron.
null is a reference. It's not an object. It is, in fact, the reference that means 'I refer to no object', and is the only reference that means 'I point at nothing'.
When you write:
MyClass x = new MyClass();
Then x is a treasure map, and new MyClass() is 'create a new treasure chest and bury it in the sand'. The = in the middle says: Update the treasure map named x, so that following it would lead you to the treasure I just made (new X() means: Make new treasure and bury it).
MyClass x = null;
means you have a treasure map named x which is currently blank.
If not, is there a way for me to create the object so that all three instance variables are null?
That would imply a treasure chest of the MyClass treasure type, which has room for some scratches (int a - a number), and which contains a treasure map (the String b variable). If you want to set them all to null, well, you can't - a is a primitive (int) and those aren't treasure maps, they are the actual number. You can't not have one - a cannot be null. At best, a can be 0. b CAN be null. That means there's real treasure, but the treasure contains yet another treasure map (in java it's mostly treasure maps all the way), but that one is blank. That's different from there being no treasure at all.
More generally, the question: "Can I make a new instance of MyClass such that all fields are some value I desire" is the wrong question to ask, perhaps: The general principle is encapsulation: MyClass is an API: It's like the receptionist at a big corp's office. The receptionist decides what services are available. Even if the CEO is available, if the receptionist elects not to answer the question 'can I see the CEO right now please?', then you can't see her.
Your question boils down to: "If I storm into this office and I demand to speak to the CEO, will I be allowed to?" - the only viable answer is: Well, the receptionist would decide, so you'd have to ask him. For classes: Whatever the API lets you do, you can do. But that's all you can do.
If there is no constructor that initializes these fields to null, then, no, you can't.
This question already has answers here:
what is the Class object (java.lang.Class)?
(7 answers)
Closed 2 years ago.
Recently I was learning annotations in java and came across Reflection API and a part of its code was:
Meta obj = new Meta();
//Get a Class object
CLass<?> c = obj.getClass();
The thing that I am not able to understand is that what is the difference between object obj and object c since both of them are just class objects.
You could treat Class object as metadata of normal object. Moreover, when you get obj.getClass() e.g. for different Integer objects, you'll get the smae Class object.
This question already has answers here:
Dynamic dispatch and binding
(2 answers)
Closed 2 years ago.
Im confused, when i use getClass( ) from a superclass reference variable that's pointing to a subclass object, the result is the subclass.
Heres a simple example:
public `class` TestGetClass
{
public static void main(String[] args)
{
Object obj = new Integer(20);
System.out.println("obj class: " + obj.getClass());
}
}
The output gives me the Integer class instead of the Object class.
obj class: class java.lang.Integer
Can someone explain please
What you're looking for is simply:
Object.class.
obj.getClass() in java could plausibly be interpreted in two different ways:
It means: Take the expression 'obj', which is a reference (i.e., a pointer). Follow the pointer and find the object it is pointing at. Ask that object what its type is.
just like 1, except, because the variable type was Object, invoke the implementation of the getClass() method from the java.lang.Object class. i.e., no dynamic dispatch.
It means: Take the locally declared variable named obj. What type did I declare it as, right here in this method? Don't care about the object/pointer at all, just the declaration.
Now, the java lang spec is crystal clear: In java, #1 is what happens. #2 is not available (you can't opt out of dynamic dispatch. As a matter of obvious language design, private methods don't do it because they don't need it, and static methods don't do it because, by being static, they just aren't a part of the hierarchy in the first place - so those seeming exceptions really don't apply. There is no other way to opt out).
Here's the thing about option #2 though: is completely pointless.
In java, you can't have mystery meat variables. Somebody declares them, and the type is written right there in the source file. There is no such thing as 'eh, figure it all out at runtime'. Even java10's var doesn't work that way (it's still locked in, for sure, at compile time).
So, you already know. It is object, what point is there to repeat it?
If you want a java.lang.Class<?> instance that represents Object, there's syntax for this. it is:
Class<?> objClass = Object.class;
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
This question already has answers here:
Using the keyword "this" in java [duplicate]
(12 answers)
Closed 9 years ago.
If the special variable this refers to an Object in a variable/method that is being used. How does it know exactly which object it must refer to out of various objects in a program?
The mechanism is almost disappointingly simple.
Each instance method actually takes one more argument than you declare for it, and that extra argument is assigned to this. Java syntax just thinly disguises this. When you write
list.get(0);
you have actually written
get(list, 0);
in a slightly modified way. The Java runtime resolves which get method to call by inspecting the type of that first argument and locating the appropriate get method in its class.
this points to the current object instance that it is used in.
If you define a class A with a method() that contains a this reference then you create two instances of the class
A a1 = new A();
A a2 = new A();
If you call a1.method() then this will refer to a1, if you call a2.method() then this will refer to a2
A a = new A();
a.doSomething(i) // is same as calling doSomething(a, i).
So, internally this refers to "a". The first argument to the function will be the object (there will only be one method that will be used by all objects). So, argument o will be the current object which has called this function.
From JAVA LANGUAGE SPECIFICATION
The keyword this may be used only in the body of an instance method,
instance initializer, or constructor, or in the initializer of an
instance variable of a class. If it appears anywhere else, a
compile-time error occurs.
When used as a primary expression, the keyword this denotes a value
that is a reference to the object for which the instance method was
invoked (§15.12), or to the object being constructed.
The type of this is the class C within which the keyword this occurs.
At run time, the class of the actual object referred to may be the
class C or any subclass of C.
The keyword this is also used in a special explicit constructor
invocation statement, which can appear at the beginning of a
constructor body (§8.8.7).
You can also refer to Oracle Tutorials
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
Oracle Java Tutorials
this is a very important keyword that can differentiate between parent and child class objects. this refers to the present context in which object has too be referred to..!!