new Obj, always or sometimes? [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 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.

Related

difference between creating an object and instantiating an object [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 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();

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

Method changing, then returning received parameter; is it good or bad? [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 3 years ago.
Improve this question
I'm working on a project where we have a method like this:
processEvent(event, context) {
// var result = some processing here
context.setResult(result);
// context's class have nothing to do with processEvent's class
return context;
}
The class SomeClass containing processEvent method has no inheritance relation with context's class (so it's not about a builder pattern here). For me, the context returning is silly because the direct caller would already have it. Are any other more plausible arguments in favor of or against this approach?
It's not uncommon to have a method return one of its parameters' values if it doesn't have any other return value. It enables code like this:
Context context = processEvent(event, new Context(/*...*/));
Whether that's good or bad style is, er, a matter of style. :-) It's not all that common, but I wouldn't say it's uncommon, either.

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?

java interface and return types [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 9 years ago.
Improve this question
I have been trying for a while, but now I'm wondering if I can. Is it possible for a method in an interface to return a variable or an array without needing two interfaces or methods(if possible)?
Every method has to specify a return type and stick with it. There are no "union types" like "returns a String or an int[]".
If you really have a method that can return two different things, you can
make a wrapper type (StringOrIntArray) that can hold both
or
have the method return Object which can be pretty much anything, including arrays, and use instanceof at the caller to see what you got.
Both options are not particularly attractive. Maybe take a step back and rethink the bigger picture.
What is wrong with
interface Foo{
int foo1();
//or....
int[] foo2();
}
?

Categories