When I run the code below, why does it throw this error?
Exception in thread "main" java.lang.CloneNotSupportedException: Student
at java.lang.Object.clone(Native Method)
at Student.clone(Student.java:44)
at StudentApp.main(StudentApp.java:10)
Here's my main class:
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("湖南省长沙市林科大","1234567",20);
Student stu = new Student("诸葛亮量",20);
stu.setAddress(address);
Student stu2 = (Student)stu.clone();
stu2.setAddress(new Address("湖南省常德市区","484848348",22));
stu2.setName("张飞飞");
stu2.setAge(23);
stu.sayHi();
stu2.sayHi();
}
This is Student class:
public class Student{
private String name;
private int age;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
#Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public void sayHi() {
System.out.println("大家好,我是" + this.getName() + "同学,我今年" + this.getAge()
+ "岁了……我的HashCode是:" + this.hashCode()+"。我家庭住址是"+address.getAddress()+",家庭住址的HashCode为:"+address.hashCode());
}
}
This is Address Class:
public class Address {
private String address;
private String tel;
private int roadNum;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getRoadNum() {
return roadNum;
}
public void setRoadNum(int roadNum) {
this.roadNum = roadNum;
}
public Address() {
super();
}
public Address(String address, String tel, int roadNum) {
super();
this.address = address;
this.tel = tel;
this.roadNum = roadNum;
}
}
From the javadoc
Invoking Object's clone method on an instance that does not implement
the Cloneable interface results in the exception
CloneNotSupportedException being thrown.
Have you tried to make your Student class implement the Cloneable interface?
Instead of attempting to use clone(), write a copy constructor that takes an instance of a Student and copies it's fields individually. Then you don't need to worry about the semantics of clone. In your example, this means having a copy constructor on Address as well, since a student has an Address field.
Read Effective Java by Joshua Bloch for reasons why clone should be avoided.
public class Student {
final private String name;
final private int age;
final private Address address;
public Address getAddress() {
return address;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student(Student copyStudent) {
this.name = new String(copyStudent.getName());
this.age = copyStudent.getAge();
this.address = new Address(copyStudent.getAddress());
}
}
From Effective Java
The copy constructor approach and its static factory variant have many
advantages over Cloneable/clone: They do not rely on a risk-prone
extralinguistic object creation mechanism; they do not demand
unenforceable adherence to ill-documented conventions; they do not
conflict with the proper use of final fields; they do not require the
client to catch an unnecessary checked exception; and they provide a
statically typed object to the client. While it is impossible to put a
copy constructor or static factory in an interface, Cloneable fails to
function as an interface because it lacks a public clone method.
Therefore you aren’t giving up interface functionality by using a copy
constructor instead of a clone method. Furthermore, a copy constructor
(or static factory) can take an argument whose type is an appropriate
interface implemented by the class. For example, all general-purpose
collection implementations, by convention, provide a copy constructor
whose argument is of type Collection or Map. Interface-based copy
constructors allow the client to choose the implementation of the
copy, rather than forcing the client to accept the implementation of
the original. For example, suppose you have a LinkedList l, and you
want to copy it as an ArrayList. The clone method does not offer this
functionality, but it’s easy with a copy constructor: new
ArrayList(l). Given all of the problems associated with Cloneable, it
is safe to say that other interfaces should not extend it and that
classes designed for inheritance (Item 15) should not implement it.
Because of its many shortcomings, some expert programmers simply
choose never to override the clone method and never to invoke it
except, perhaps, to copy arrays cheaply. Be aware that if you do not
at least provide a well-behaved protected clone method on a class
designed for inheritance, it will be impossible for subclasses to
implement Cloneable.
Student needs to implement Cloneable.
Student and Address both need 'deep' clone, if you don't want to share Address instance both in source and target Student instances:
class Student implements Cloneable {
#Override
protected Object clone() throws CloneNotSupportedException {
Student result = (Student)super.clone();
result.setAddress((Address)getAddress().clone());
return result;
}
...
}
class Address implements Cloneable{
#Override
protected Object clone() throws CloneNotSupportedException {
return (Address) super.clone();
}
...
}
Related
I have to classes
public class Consumer{
private String name;
private int salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
and next
public class Donor {
private String name;
private int amount;
private String location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
now i have another class which contains a method method1()
public class GenericClass<T> {
public void method1(List<T> list){
Iterator i = list.iterator();
while (i.hasNext()){
}
}
}
and My main method is
public class MainMethod {
public static void main(String[] args) {
List<Donor> d = new ArrayList<>();
Donor donor = new Donor();
donor.setAmount(500);
donor.setName("bill");
Donor donor1 = new Donor();
donor.setAmount(1250);
donor.setName("linda");
d.add(donor);
d.add(donor1);
GenericClass genericClass = new GenericClass();
genericClass.method1(d);
}
}
i want to make this method1() dynamic and return a dynamic result.
so if i send the list of Consumer then it should return me the sum of all salaries and if i send the list of Donor then it should send me the sum of amount donated ?
how can this be achieved ?
First, you'd probably not make the class GenericClass generic but the method method1().
Then you could provide a ToIntFunction<T> which takes an object of type T and returns an int value. Thus your method could look like this (Java8 code):
public <T> int method1(List<T> list, ToIntFunction<T> transformation){
return list.stream().collect(Collectors.summingInt(transformation));
}
You'd then call that method like this:
int sumSalaries = method1(consumers, Consumer::getSalary);
int sumDonations = method1(donors, Donor::getAmount);
Pre-Java8 code would be possible as well but it would be a little bigger (you'd need to provide ToIntFunction, implementations of that interface and a slightly larger method body).
Alternatively you could use an Interface that's implemented by both classes but that would require you to use a common method name (e.g. getAmount() or getIntValue() etc.)
I have two packages lets give them the name package 1 and package 2.
Class A and Class B is in package1. Class A contains an ArrayList called PTable. Class B contains a function called query() that filters through PTable,in Class A, based on a certain conditions and returns an ArrayList called result that contains all the elements from PTable that meet that condition.
I now have package2 that contains Class C. Class C imports Class B from package 1; Class C is a subclass of HttpServlet. I create an object of Class B in class C and initializer it.
I then call the function query() and assign it to a variable called results. When I try and get the properties of an element at a certain index, I can't see the properties of the original objects stored in the ArrayList PTable.[This is what appears when I try and access the properties of the objects. My aim is to see the second image ][1]
Nice to ask questions but first spend sometime studying Java. Read a book or online and you will learn about casting very quickly. Also about classes, super classes etc
Your storing the objects in a variable of type Element (your results array list).
Cast the object back to the type it belongs too and then you will see the variables.
Code design note : storing different types of classesin the same array list is legal and possible but bug prone. Try to avoid it. If you change the order of storing variables into the list, you need to change all the access code too. Anyway happy learning.
There are free online Java tutorials study them -> https://www.google.co.in/search?q=java+tutorial+beginner
Sample class, in the main method try to get the object at position 1 and cast it to a Person :
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Car {
private String manufacturer;
private String model;
private double price;
private int yearOfMfr;
private Date dateBought;
private String licenceNumber;
public Car() {
super();
}
public Car(String manufacturer, String model, double price, int yearOfMfr, Date dateBought, String licenceNumber) {
super();
this.manufacturer = manufacturer;
this.model = model;
this.price = price;
this.yearOfMfr = yearOfMfr;
this.dateBought = dateBought;
this.licenceNumber = licenceNumber;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getYearOfMfr() {
return yearOfMfr;
}
public void setYearOfMfr(int yearOfMfr) {
this.yearOfMfr = yearOfMfr;
}
public Date getDateBought() {
return dateBought;
}
public void setDateBought(Date dateBought) {
this.dateBought = dateBought;
}
public String getLicenceNumber() {
return licenceNumber;
}
public void setLicenceNumber(String licenceNumber) {
this.licenceNumber = licenceNumber;
}
}
public class DemoApp {
public static void main(String[] args) {
List<Object> results = new ArrayList<>();
DemoApp app = new DemoApp();
app.fillItUp(results);
Car acar = (Car) results.get(0);
acar.setLicenceNumber("Flying Duck");
}
private void fillItUp(List<Object> results) {
Car car = new Car("sel2in", "electric_VTOL", 540923, 2018, new Date(2018, 3, 32), "Skyprog");
results.add(car);
results.add(new Person("tushar", 39));
}
}
I need to create objects that will be visible in the whole program (in various classes). I do not want to create Singletons for such classes because I might need a few instances. So I created a class of type Singleton as a container and inside it I created objects that I can use at any point in the program.
Below is a sample code where you see what's going on. Referring to an instance of Container, I will always have access to the facilities created there. I'm building a program to study. There will be big but I came up with the idea that has solutions to the problem. The problem was that the different controllers want an explicit object there any data. And then based on the full object perform calculations.
public class Person {
String name;
String surname;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public Person(){
}
}
.
public class Container {
private Person person1;
private Person person2;
private static Container singleton = new Container( );
public Container() {
person1 = new Person();
person2 = new Person();
}
public Person getPerson1(){
return person1;
}
public Person getPerson2(){
return person2;
}
public static Container getInstance( ) {
return singleton;
}
}
.
public class Test {
public static void main(String[] args) {
Person person;
Container singleton = Container.getInstance();
person = singleton.getPerson1();
person.setName("John");
Person personTest1;
Container singleton2 = Container.getInstance();
personTest1 = singleton2.getPerson1();
System.out.println(personTest1.getName());
}
}
Output:
John
It might not be the best solution but have not found as simple and quick solution as this. What do you think?
I have just some remarks : Container which is your factory of Person should not be instantiable by client. Otherwise, client could create multiple versions of person1 and person2 .
Imagine this code :
Container singleton = Container.getInstance();
Person person = singleton.getPerson1();
person.setName("John");
Container singleton2 = new Container();
Person personTest1 = singleton2.getPerson1();
System.out.println(personTest1.getName());
To avoid that, you should make the constructor private.
public class Container {
...
private Container() {
person1 = new Person();
person2 = new Person();
}
...
}
I'd use an enum here.
The code is cleaner and it is easier to extend with more Objects as you don't need to create a getter every time you add another.
As a bonus you don't have to worry about thread safety, eager initiation or exposing constructors.
public enum Container {
PERSON1(new Person()),
PERSON2(new Person()),
PERSON3(new Person()),
PERSON4(new Person()),
PERSONN(new Person());
private final Person singleton;
Container(final Person person) {
this.singleton = Objects.requireNonNull(person);
}
public Person get(){
return singleton;
}
}
public static void main(String[] args) {
Person singletonA = Container.PERSON1.get();
singletonA.setName("John");
Person singletonB = Container.PERSON1.get();
System.out.println(singletonB.getName());
}
I am doing a project based around the concepts of inheritance and have created a super constructor which has two variables within itself (String, int), this super constructor is then called within a sub constructor that inherited the super constructors class. I then use two methods to return the properties of those variables within the constructors. The age property is outputting fine but the String property is returning null. Here's the code:
Animal super-class
abstract public class Animal
{
int age;
String name;
Animal(String name, int age)
{
this.age = age;
this.name = name;
}
Animal()
{
this("newborn", 0);
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
}
Wolf sub-class
public class Wolf extends Carnivore
{
String name;
int age;
Wolf(String name, int age)
{
this.name = name;
this.age = age;
}
Wolf()
{
super();
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
Main method class
public class Main {
public static void main(String[] args)
{
Wolf newWolf = new Wolf();
System.out.println("Name = " + newWolf.getName());
System.out.println("Age = " + newWolf.getAge());
}
}
Age is returning as 0 which is correct but System.out.println("Name = " + newWolf.getName()); seems to be returning null instead of "newborn". Any help on resolving this issue is appreciated thanks.
Update - I need the getName() method for another constructor that I haven't included in this example so is there a way to have them both exist?
The issue here is that you are defining your fields in the sub-class, you don't need to as they are inherited from the parent.
Your class has two sets of fields, one from the super (these are the ones set by your constructor, which is calling super() and the other from the child class (these are the ones returned by your getters, which are not initialized. the zero is int's default, not set either).
So simply remove the fields definition from the child class
I have a couple to class in which I'm getting and setting a few things and then finally calling it in my main method. But when I call my class in the main method it just gives me the object instead of name,address and age. I know this structure is very complicated but I want to keep this structure because later on I will be adding a lot of things to this. It would be AMAZING if someone could tell me how to do this. I would really appreciate this. Below is my code for all my classes
This is my first class
public class methodOne
{
public String getName()
{
String name = "UserOne";
return name;
}
public int getAge()
{
int age = 17;
return age;
}
public String getAddress()
{
String address = "United States";
return address;
}
}
This is my second class
public class methodTwo
{
String name;
String address;
int age;
public methodTwo(methodOne objectOne)
{
name=objectOne.getName();
address=objectOne.getAddress();
age=objectOne.getAge();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
This is my third class
public class methodThree {
private methodTwo methodTwoInMethodThree;
private methodOne methodOneInMethodThree;
public methodThree()
{
this.methodOneInMethodThree = new methodOne();
this.methodTwoInMethodThree = new methodTwo(methodOneInMethodThree);
}
public methodTwo getMethodTwoInMethodThree() {
return methodTwoInMethodThree;
}
public void setMethodTwoInMethodThree(methodTwo methodTwoInMethodThree) {
this.methodTwoInMethodThree = methodTwoInMethodThree;
}
}
This is my fourth class which is the method maker
public class methodMaker {
public methodThree brandNewFunction(methodTwo object)
{
methodThree thirdMethod = new methodThree();
thirdMethod.setMethodTwoInMethodThree(object);
return thirdMethod;
}
}
This is my main class which calls methodMaker. What I want to achieve is that when I print the value it should print the name,address and age but instead it just prints trial.methodThree#4de5ed7b
public class mainClass {
public static void main(String args[])
{
methodMaker makerOfMethods = new methodMaker();
methodOne one = new methodOne();
methodTwo object = new methodTwo(one);
System.out.println(makerOfMethods.brandNewFunction(object).toString());
}
}
What you need to do is to override the default implementation of the .toString() method in the objects you want to print out:
#Override
public String toString()
{
return "Name: " + this.name;
}
EDIT:
I do not know exactly where you are printing, and you naming convention doesn't really help out, but from what I am understanding, you would need to implement it in all of you classes since they all seem to be related to each other.
So, in your methodOne class (can also be applied to methodTwo):
#Override
public String toString()
{
return "Name: " + this.name + " Age: " + this.age + " Address: + " this.address;
}
In your methodThree class:
private methodTwo methodTwoInMethodThree;
private methodOne methodOneInMethodThree;
#Override
public String toString()
{
StringBulder sb = new StringBuilder();
if(this.methodTwoInMethodThree != null)
{
sb.append("Method 2:").append(methodTwoInMethodThree.toString());
}
if(methodOneInMethodThree != null)
{
sb.append("Method 1:").append(methodOneInMethodThree.toString());
}
return sb.toString();
}
When you call
MyClass myObject = new MyClass();
System.out.println(myObject);
Implicitly , java calls instead
System.out.println(myObject.toString());
So, if in MyClass, you override toString(), then whatever your toString method returns is what's gonna be printed.
Side note: are you confusing classes and methods? Methods are functions in your classes, classes are wrappers around a bunch of attributes and methods. Your naming is confusing.
try this code:
public class methodTwo
{
String name;
String address;
int age;
public methodTwo(methodOne objectOne)
{
name=objectOne.getName();
address=objectOne.getAddress();
age=objectOne.getAge();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString(){
return name+" "+address+" "+age;
}
}
Are you printing the object using println()?
From the docs, println():
calls at first String.valueOf(x) to get the printed object's string value
This string value is obtained from the object's toString() method, which:
returns a string consisting of the name of the class of which the object is an instance, the at-sign character `#', and the unsigned hexadecimal representation of the hash code of the object
So if you want to print anything other than this you have to override the toString() method in your object and return a string containing whatever you want.
Just google "override tostring java" and you will see a ton of examples.