Reputation: 133
Sample code:
PrintMethod.java
public class PrintMethod {
void print (String s) {
System.out.print(s);
}
}
PrintS.java
class PrintS {
public static void main(String[] args) {
PrintMethod pm = new PrintMethod(); //i know this is how you make a new object, but
pm.print("Hello");
}
}
Where is the object here? I've looked all over the internet, but what my teacher is telling me is different from what I found. Help?
And what then is the difference between an object and a class? If PrintS
and PrintMethod
are objects as well? I thought an object was an instance of a class? I'm so sorry, I just need this topic to be crystal clear.
Upvotes: 1
Views: 368
Reputation: 1481
An Object
is the blueprint of a Class
that gets created in Heap Memory. To get access to those objects we use references
in Java Code.
So we don't directly access objects in Java Code. It is the reference
that we access and use.
In your case pm
is just an reference
to a instance of PrintMethod
placed in Heap memory. But "Hello"
is a literal instance of a String
that gets created in String
pool.
Upvotes: 2
Reputation: 1
Objects are building blocks of an OO program. A program that uses OO technology is basically a collection of objects.
Each Object is made up of the Data & Behavior.
Object Data: the Data stored within an object represents the state of the Object. In OO programming terminology, this data is called Attributes.
Object Behavior: The behavior of an object is what the object can do. In OO programming terminology, these behaviors contained in methods, and you invoke a method by sending a message to it.
Also i agree with the "Maroun Maroun."
Upvotes: 0
Reputation: 1
We can define Object in 4 ways .
1) An Object is an Instance of a class ( Instance is nothing but allocating sufficient amount memory space for the data members and methods of a class )
2) Each and every class variable are also known as objects .
3) Blue print of a class is known as an object .
4) Each and every grouped Item is known as an object ( You know a grouped item is a variable which allows us to store multiple values of same type or different type or both at once )
So.,
Here i will give a small explanation about what is a object .
dear friend " Have you seen a Tree" ?????
Most of the people say that they have seen a tree . But Answer is Nooooo....
You never seen a tree but you have seen Types of tree like Banana Tree, Apple tree... etc
So here if you understand there is a very good beauty in it .
So a tree is a plan and Types of trees are Objects . So, Now tell me does a tree exists . Nooo its just a plan and based on that plan types of trees can be planted .
A class is a blue print and based on a class many objects can be created . So Always remember that "A class has Logical Existence " and "an Object has Physical Existence "
Upvotes: 0
Reputation: 95968
I think the best answer you can get can be found in the official docs:
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.
Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class. The new operator returns a reference to the object it created.
In your case, pm
is a reference to an object of type PrintMethod
. When you do pm = new PrintMethod()
you are constructing a new object.
See the part of Using Objects in the official docs.
Clarification: When you say class, you usually refer to the code, it is simply a piece of code. But when you say object, you mean an instance of the class. Every object "belongs" to a class.
For example, consider a class named Car
. All cars have wheels. An instance of Car
would be a specific car, say toyota
. So toyota
is now an instance of Car
.
Upvotes: 3
Reputation: 1451
PrintMethod pm = new PrintMethod();
the left side part is called as Declaration and the right side part is called reference,where new
which allocated the memory>
Upvotes: 0