Wrapping an Integer [closed] - java

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 4 years ago.
Improve this question
I need to make a constructor that accepts an integer to be wrapped. I currently have:
public class IntegerRateable implements Rateable {
private Integer object;
public IntegerRateable(Integer object) {
this.object = new Integer(object);
}
I am unsure what is wrong with my code. I was under the impression that this should allow it to be wrapped.

If what you want is, your IntegerRateable class to contain an Integer as member variable, try this:
public class IntegerRateable implements Rateable {
private Integer object;
public IntegerRateable (int number) {
this.object = number;
}
Then, just instantiate IntegerRateable object as:
IntegerRateable integerRateable = new IntegerRateable(5);

You may first want to check your braces, you're missing a closing brace }.
If that's not the problem, I'am assuming that the problem is in this line
this.object = new Integer(object); . There is nothing wrong with this, it's just that it using the Integer constructor has been deprecated which you can find out more about here https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#Integer-int-
So the preferred way of doing it would be to do this.object = new Integer.valueOf(object);
EDIT: Although, I don't see a point in doing this since you can just do this.object = object;

Related

Java class has only one constructor, no parameter for constructor, all instances of that class will point to the same object in the heap? [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
Have the following case:
class Bond {
private static int price = 5;
public boolean sell() {
if(price<10) {
price++;
return true;
} else if(price>=10) {
return false;
}
return false;
}
public static void main(String[] cash) {
new Bond().sell();
new Bond().sell();
new Bond().sell();
System.out.print(price);
Bond bond1=new Bond();
System.out.print(price);
}
}
It will print: 8 9.
Will all instances that will be made, will point to the same object in the heap?
There's a very simple rule in Java: new SomethingOrOther() will always create a new object (unless it somehow produces an exception).
So the answer is obviously: no, the main method you posted will create 4 instances of Bond.
Those instances happen to not have any fields that makes them different in any interesting way, but they are distinct instances.
The reason it "looks like" only one instance exists is that your price field is static, which means it belongs to the class Bond itself and not to an individual instance, which also means there's only one price ever, no matter how many instances you have (yes, even if there are no instances at all).
Remove static keyword of "price" to answer it yourself.
static variables hold the same value across all the object instances.

How to use object array in other file in the same package? [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 4 years ago.
Improve this question
This is file theArray.java
public class theArray<E> {
private Object[] arr;
public void add(E item){
//adding new element method omitted
}
This is the other file named Calculate.java (which is in the same package with theArray.java)
public class Calculate {
//I want to bring the Object array arr that I made in file theArray.java but syntax is really confusing.
}
How can I resolve these problems?
First, your myArray class shouldn't hold an array of Objects, but rather data of type E:
private E[] arr;
This should work as long as the files are in the same package:
theArray<Integer> myArray = new theArray<>();
To add an element should be simple as well (as long as your implemented add method works):
myArray.add(1); //adds 1 to the array
Let me know if this works as I cannot test it right now. Good Luck!
In order to access an array in a different class you have to assign the array to a object in that class. To do this you would need to build a constructor like such.
public void theArray() {
int [] array = {};
}
From here you would then be able to call the constructor in your other class and be able to add methods in theArray class to add values to the array. Hope this helps

returning a array object from class object [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
public class abc<E>{
Arraylist<E> returnthis = new Arraylist();
public abc<E> fuction(E c)
{
returnthis.add(c);
return returnthis
}
}
// main
abc array = new array();
temp = array.add(4);
How can i return an arraylist from a function which return object of a class.
If I understand your question, then you will need to add an accessor (or getter) method to your abc class -
List<E> returnthis = new ArrayList<>(); // <-- note caps and diamond operator.
public List<E> getList() {
return returnthis;
}
Then you can
abc<Foo> array = new abc<>(); // <-- abc is generic
List<Foo> foos = array.getList(); // <-- like so
Finally, abc is a very poor class name. It's nondescript and doesn't follow Java capitalization conventions.
Your code is hard to read. It might be why you're having trouble understanding it.
You've got to make the return type match the variable you're returning.
public class Demo<E> {
List<E> data = new ArrayList<E>();
public List<E> addToData(E c) {
data.add(c);
return Collections.unmodifiableList(data);
}
public String toString() { return this.data.toString(); }
}
First of all you don't seem to have the basics down of arrays quite yet as bits of that isn't right. First you need to understand returning an array object correctly. Here is an example:
ArrayList<E> myArray = new ArrayList<E>();
public ArrayList<E> getMyArray(){
return myArray;
}
Secondly capitalize the class name my OCD is going crazy right now.
Now look at your array, it doesn't hold values of the class you just created if thats what your trying to do. I am not entirely sure what it is your trying to do yet though you need to fix up your code first.

How to find if a field is an enum array using reflection? [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 8 years ago.
Improve this question
I want to find out in Java, using reflection, if a particular field is an array of enums. Does anyone know how to do this?
First check if the field is an array with field.getType().isArray(), and then check that the array component type is an enum with field.getType().getComponentType().isEnum()
As Holger mentioned in his comment a simple Enum[].class.isAssignableFrom will do it.
import javax.swing.SortOrder;
public class Main {
private SortOrder[] sortOrders;
public static void main(String[] args) throws SecurityException,
NoSuchFieldException {
Field sortOrdersField = Main.class.getDeclaredField("sortOrders");
System.out.println(isEnumArray(sortOrdersField));
}
private static boolean isEnumArray(Field field) {
Class<?> type = field.getType();
return Enum[].class.isAssignableFrom(type);
}
}

How to add multiple values to an Object ArrayList? [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
The problem should be on the createClass() method... is it wrong to use classes.add(this)?
private static ArrayList<VirtualClass> classes = new ArrayList<VirtualClass>();
private boolean isPrivate;
private String className;
private String methodName;
public void setVirtualClass(String name, String method, boolean isP){
this.className = name;
this.isPrivate = isP;
this.methodName = method;
}
public void createClass(String name, String method, boolean isP){
this.className = name;
this.isPrivate = isP;
this.methodName = method;
classes.add(this);
}
More details on the problem: Failed to store values in ArrayList of class object. (CODE EDITED)
I suppose you want to keep a record of all created classes?
It is generally bad practice to pass out this in a constructor because, by definition, this hasn't been constructed yet. In this instance I don't think it will cause problems but it certainly doesn't smell right.
I would consider using a static factory method or another form of factory pattern so that you can split up object creation and the storing of instances.
No one can tell you whether it is right or wrong unless you tell what you are trying to do.
If what you are doing is something like : (assume your class is called Foo)
Foo foo = new Foo();
foo.createClass("Blablabla", "method1", true);
foo.createClass("AnotherClass", "method2", true);
something like that, then yes, you are probably wrong. Because what you are doing is simply changing the only Foo instance with different attribute, and adding the same object to the list.

Categories