About 397,000 results
Open links in new tab
  1. What are all the different ways to create an object in Java?

    Sep 18, 2008 · This is the most common way to create an object in java. Almost 99% of objects are created in this way. Employee object = new Employee(); Method 2. Using Class.forName(). Class.forName() gives you the class object, which is useful for reflection. The methods that this object has are defined by Java, not by the programmer writing the class.

  2. How do I copy an object in Java? - Stack Overflow

    Mar 23, 2012 · Basic: Object Copying in Java. Let us Assume an object- obj1, that contains two objects, containedObj1 and containedObj2. shallow copying: shallow copying creates a new instance of the same class and copies all the fields to the new instance and returns it. Object class provides a clone method and provides support for the shallow copying.

  3. Java Moving an Object Across the Screen - Stack Overflow

    Oct 28, 2013 · I am trying to move a train across my java window and am having serious problems. I have a Train class in which I made the train, and a Driver class which is supposed to move the train. I need to make the whole train move from right to …

  4. What is the purpose of creating static object in Java?

    Jul 6, 2018 · There is no such thing as a static object in Java. The variable that points to the object can be static, but the idea of an object being static has no meaning. The purpose of a static variable or any other static type member is to attach the member to the type itself rather than to an instance of the type.

  5. clearing or set null to objects in java - Stack Overflow

    Aug 2, 2013 · As soon as the scope of the variable ends the object becomes eligible for GC and gets freed up if no other reference points to the object. Java is pass by value so if you set the list as null in the method then it will not affect the original reference that was passed to …

  6. java - How do you make a deep copy of an object? - Stack Overflow

    Sep 15, 2008 · There is no easy answer here. If you want to deep copy an object you will have to traverse the object graph and copy each child object explicitly via the object's copy constructor or a static factory method that in turn deep copies the child object. Immutables (e.g. Strings) do not need to be copied. As an aside, you should favor immutability ...

  7. immutability - Make immutable Java object - Stack Overflow

    Here are few rules, which helps to make a class immutable in Java : 1. State of immutable object can not be modified after construction, any modification should result in new immutable object. 2. All fields of Immutable class should be final. 3. Object must be properly constructed i.e. object reference must not leak during construction process. 4.

  8. Is it possible to make an object "Read Only" to a method

    May 10, 2012 · Make your object immutable by default. Any operation on the object returns actually a copy of the object. Also, note that the API in the SDK have sometimes methods that return an immutable version of an object, e.g. Collections.unmodifiableList. An attempt to mutate an immutable list will throw an exception.

  9. How can I use pointers in Java? - Stack Overflow

    Java does have pointers. Any time you create an object in Java, you're actually creating a pointer to the object; this pointer could then be set to a different object or to null, and the original object will still exist (pending garbage collection). What you can't do in Java is pointer arithmetic.

  10. How to create immutable objects in Java? - Stack Overflow

    Jun 10, 2011 · An immutable object is an object that will not change its internal state after creation. They are very useful in multithreaded applications because they can be shared between threads without synchronization. To create an immutable object you need to follow some simple rules: 1. Don't add any setter method

Refresh