difference between creating an object and instantiating an object [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 last year.
Improve this question
I'm not able to understand difference between creating an object and instance of an object.
so when you say creating an object in java, we create it like object_name = new constructor();
so what's happening behind the scenes as well?

As I understand it instances of objects are specific realizations of the class they are created from. When an object is instanced it begins to physically occupy memory of the system. The way I understand it is the class = blueprint of the house and then instanced object = the actual physical house built from it.
I hope this helps I am still quite new to all this also!
To answer your question there is no difference between "creating an object" and "instantiating an object". Both mean one and the same.

Creating an Object and instantiating an Object is the same thing

it`s not equal, when create an object is new an object(like A), new is an operator,you can not point this object to a veriable; but where intancing an object, you should point this object an varible like this:
Point user = new User();

Related

Don't I lose functionality by coding to an Interface? List vs ArrayList [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 1 year ago.
Improve this question
(1) List<?> myList = new ArrayList<?>();
(2) ArrayList<?> myList = new ArrayList<?>();
I understand that with (1), implementations of the List interface can be swapped. It seems that (1) is typically used in an application regardless of need (myself I always use this).
I am wondering with (1) I don't have specific ArrayList functionality right? I only have the general List functions. So I don't have access to anything array list specific? And if so? How can I get access to ArrayList specific functionality? Do I swap the List with an ArrayList or can I cast the List to an ArrayList?
You typically expose the interface to the public and keep the specific implementation to yourself. This is to make swapping it out with something different easier, as consumers only know that it is a list. If you only use the reference within your own object, then there's no need to hide the specific implementation. If you need an array list then for all means do store the reference as an arraylist!

Can we create an object in java without a class? [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 1 year ago.
Improve this question
If you can please help me understand if this is possible to create an object in java without having a class.
Not possible, it's either a primitive or an instance of a class.
In Java, Objects are a copy of Class (or better - instance of it).
So there can't be objects without classes.
But there is a predefined class Object which all classes inherit and therefore all objects are of type Object.
Maybe you can try
Object myObj = new Object();
if that helps for you

Java: Create Instances based on prototype [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
How can I, and What is the best/proper way (ie, most performant and clearest syntactically) in Java to create object instances based on a prototype object instance, when this will occur repeatedly and in a performance critical code path?
I have thought about cloning via a cloning support library, but is that the best/only way? (These need to be arbitrary objects, btw, not ones that implement Clonable).
To clarify what I mean: I have an existing instance of Class T, which has fields set on it, and I want to pop out many versions of the same object to use separately, with the best performance and syntactic clarity possible.
Thanks.
Create a builder, which receives this class instance:
Person newOne = new PersonBuidler(oldOne).setAge(42)
Implementation of this builder may use apache common BeanUtils for cloning Java Beans or some other utility library for cloning arbitrary class.
See How do I copy an object in Java?

new Obj, always or sometimes? [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 7 years ago.
Improve this question
Should I always make new objects out of everything in java even if i know i will only use one?
obj = new obj();
obj.method();
or just
classOfobject.method();
Why should I bother with the creation of a new obj in a situation like this?
classOfObject.method() only works if method is static.
Having said that, if your class truly represents a "thing", be that a user, a car, whatever, then you should make the method non-static and use new to make an object and invoke information on it.
If the class is a container for methods that don't have anything to do with objects themselves, e.g. a Utils class like StringUtils, then you can use static methods, e.g. StringUtils.toUpperCase(), and be happy with it.

Class design - create a object from String data? [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 9 years ago.
Improve this question
I am working on simple application of Universal Turing Machine. I have a data to create machine from, my questions is not about UTM I would like to illustrate it only on this app. I need to create an object from String data, e.g. this method create one transition function of my UTM:
public static Transition createFromData(String data) {
Transition trans = new Transition();
String[] dataSplitted = data.split("1");
trans.setInputState(new State(dataSplitted[0]));
trans.setInputSymbol(dataSplitted[1]);
trans.setNewState(new State(dataSplitted[2]));
trans.setNewSymbol(dataSplitted[3]);
trans.setMovement(Movement.getByCode(dataSplitted[4]));
return trans;
}
Is it good idea (from class design perspective) to have such methods in Transition class or should I separate them to another / tool classes? What is bets practice for this issue?
Using tools/utils classes always remind me of structured programming ;) but would help if you just had a constructor on the Transition class the would receive the raw data and create a new transition object with that. Since you will be creating a new transition each time you find some raw data the best fit is in the constructor (or maybe in a factory method, but that is another discussion)
what about having this method in a another class for eg: RawData, which will have static factory methods for creating different objects from data.
In your case you can have one method will will return Transition object and then you can have multiple methods and each return a different object depending upon how data is organized / represented in that object.

Categories