I have exported an ArrayList of instances of class defined by myself, and I need to read in another project. I don't intend to use the methods of that class in particular, only to use its atributes.
I could transform those atribuites into ArrayList of primitive objects, that I could read afterwards without the need of having that class implemented again in that project, but I'm specifically asked not to do so. Is there a way to get only the atributes of such class?
Related
I'm currently learning java in order to make an app on android and I checked that swift has a structure that stores information in memory. I'd like to know, if in java this type of object exists, because the class storage the reference on the memory. Also I checked that Kotlin has a data class, does java have a similar object?
No, there is nothing like that, but there are tools, that try to mimic this behavior, for example lombok. Using #Data annotation we're getting default constructor, getters, setters, toString, equals, hashCode. We can fine-tune it by using annotations like #Getter, #NoArgsConstructor etc.
Neither Java nor Kotlin have anything similar to those Swift types you are talking about. Assignment always copies references to an object, rather than the object itself. What Kotlin's data classes do is that they create a copy method (among other things) that allows you to explicitly make a copy of an object, but you still have to actually call the method.
val b = a // b and a point to the same object, even if it is a data class
val b = a.copy() // this is what you need to do to create a copy of a data class
Java assignment copies references, not objects, and the same is true for Kotlin. There is no way around this, because it is a feature of the language itself. Copy constructors and methods (like what Kotlin's data class gives you) are the closest thing you have to such a feature. To get something like this in Java without having to manually write the code everytime, you could look into Project Lombok.
Starting with Java 14 you will have access to Record immutable class. It is similar in concept to data class in Kotlin.
I was wondering whether Class instances are immutable. The declared methods names do not suggest that the instance state is changed when they are invoked, but I found no explicit guarantee on the javadoc.
Scenario: I need to store unique class names in a Set. Ideally, I would like to populate the set with Class instances to avoid unnecessary calls to Class.forName() when I need to access the classe via reflection. However, it preferable to use immutable objects as keys of sets. Hence I was wondering if I could use Class instances right away.
First, The generics part Class<?> really doesn't matter here. Sure, no raw types, so Class<?> is better than Class, but for your question, the wildcard doesn't matter.
So in essence, you are asking whether Class objects are immutable. And for all practical purposes, they are.
Class objects come into existence when a class loader loads a class, and they stay put unless the whole class loader is unloaded, and everything it loaded with it.
Which can't happen when such class objects are still used in a map somewhere.
On the other hand: Class.forName() shouldn't be too expensive for classes already loaded. And when things such as serialization come into play, people suggest to go with String instead of Class objects for example (see here).
One has to distinguish between the immutable identity of a class object, and the actual "code" belonging to the class. That code can be changed at runtime (by instrumentation, think hot swap of code). But the class name, and its each code, and equals() equality should not be affected by that. Because the "identity" stays the same.
Final note: as the interesting comments below lay out, there are certain ways to alter Class objects to a certain degree. But all of these activities are definitely "out of the norm". Therefore: theoretically, you might prefer Strings over Class objects, but practically, in "normal" applications, using Class should work fine, too.
As I don’t really agree with other answer I decided to write this one,
Classes are not immutable, but they are unique - only one instance of Class object can exist for one class.
BUT class it not defined by its name, as classes might be from different class loaders, and different class loaders might have classes with same names - but that will be different classes, you would get ClassCastException if you would pass some object between code handled by 2 different class loaders if that object type would exist in both of them (as separate one, not inherited).
Class instances can be still safely used in Set, as they use default implementation of hashset/equals so only same instances of Class will be considered equals.
But to decide if you should use String or Class you need to know how exactly your app is supposed to work, as like I said, multiple classes with same name can exist between different class loaders.
And by just storing class name you can’t be sure that Class.forName will return same instance as expected it might even load some other class with same name from current class loader instead of using expected one.
I am making a modification and a separate application that allows replays to be saved for a certain game.
What I have to serialize and deserialize is an 2 arrays of class ContO, arrays of class Plane, Trackers, and a class Medium, and that is no problem.
To extend this functionality, I decide to reconstruct it in the separate application so that it supports 2 versions of said game. The way I plan to do this is to use abstract classes named Medium, ContO, Plane, and Trackers, and the classes that will extend those will be named things like MediumVersion1 and MediumVersion2, ContOVersion1, and so on.
In the original game files the class is named ContO, Plane, Trackers, and Medium for both versions, and what I wonder is: by changing the name of the class to reflect the version of the file that will be deserialized, will it effect the deserialization process?
For example, I serialize the class as the name of ContO in the original game files, but deserialize it under a new class name named ContOVersion1, but contains the exact same variables.
I just tried this and the answer is you cannot change the class name. You will end up with a ClassCastException when you try to cast the object you get back from ObjectInputStream.readObject() into your new class with a different name. This is the case even if you keep the same serialVersionUID on both classes.
You can definitely not do this.
The original class and package names are encoded along with the data, and the incoming object is constructed as that class, so, to avoid a class cast exception, what you cast it to must be identical, as must many other aspects of the class - but not all of them. See the Object Versioning chapter of the Object Serialization Specification for more information.
Hello i am trying to create a class called ParcelableObject in which i implement the Parcelable interface.
Then i will create other objects, which they will extend ParcelableObject.
I am doing it because i don't want to write the methods of Parcelable interface in each object.
As for now i have managed to do this for Objects containing other Objects (which all of them extend the ParcelableObject class) and for primitive Types.
I am having troubles doing it for Arrays and List of primitive Types and of course Arrays and Lists of Objects
I intend to expand this to cover Lists, Arrays.
The way i did it is by getting all the fields of the object given
So here is my question.
First of all
Is this possible?
Second i tried to expand it for arrays or lists of primitive types and i fail to do it.
To be more specific. Now i am trying to expand it to Arrays of Integers, or Lists of Integers.
As i see i fail to read from the parcel a List of Integers because i get cast errors etc.
So any help would be grateful.
Also the basic idea around this is to store to the ParcelableObject the object of its subclass.
Then if i get an object (not primitive type) i write its class name to parcel and then the object.
So when i want to read an object from the parcel, i read first the string with its class name, make a new instance of the class using (Class.forName() etc) and then assign the object read from parcel to the Object instantiated above. If there is any better way to get the Class Name of the ParcelableObject's subclass other than filling the parcel unnecessary Strings id like to know.
Here is the ParcelableObject.java
You can download the whole android ParcelableObject project to test it. Demonstration is included.
Tested it under virtual device Nexus S (4.2). Project is targeted to work for 4.0 and above.
https://github.com/tchar/ParcelableObject/blob/master/ParcelableObject/src/com/parcelableobject/ParcelableObject.java
UPDATE*** Added Support for primitive Object Lists.
Still having problems with primitive types (int, etc) not with Integers and list of ParcelableObjects
Thanks in advance.
I am using an API that gives access to a certain set of subclasses with a common interface. I use the interface throughout my code, and the instances are resolved to the proper subclass based on user needs. My problem is that I need to create a copy of one of these objects, but I don't have access to the clone() method and the API doesn't provide a copy constructor.
ie:
ObjectInterface myObject = objectFromParameter.clone(); //Not possible...
Is there a workaround in Java?
iYou might be able to do what you want with reflection. Alternatively, If the object supports serialization, you can serialize to a byte array and then reconstruct a new instance from that.