Is there a bug in this java code?

class Creature { private int yearOfBirth=10; public void setYearOfBirth(int year) { yearOfBirth = year; } void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; // is this correct it compiles fine } int getYearOfBirth() { return yearOfBirth; } public static void main(String args[]) { Creature c = new Creature(); c.setYearOfBirth(89); Creature d = new Creature(); c.setYearOfBirth(d); System.out.println(c.yearOfBirth); } } 

Is there an error in this code?

Is "other.yearOfBirth" incorrect? My teacher says this is wrong, but it works great for me.

+6
java
source share
7 answers

As written, it will work as you discovered. I suspect there is a fundamental misunderstanding in the game.

My psychic abilities tell me that your teacher expected the code more than the following:

 class Creature { private int yearOfBirth=10; public void setYearOfBirth(int year) { yearOfBirth = year; } public void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; } public int getYearOfBirth() { return yearOfBirth; } } class Program { public static void main(String args[]) { Creature c = new Creature(); c.setYearOfBirth(89); Creature d = new Creature(); c.setYearOfBirth(d); System.out.println(c.yearOfBirth); // This will not compile } } 

It is not clear that you have created only one class - your main application class. This actually makes yearOfBirth a kind of hybrid global value that you can access from the main method. In more typical constructs, Creature is a class that is completely independent of your main method. In this case, you should only access Creature through your public interface. You cannot directly access your private field.


(Pay attention to all pedants: Yes, I know that I simplify.)

+7
source share

You should ask your teachers to explain why they think this is wrong (perhaps this is a matter of style or even misunderstanding), so you can learn from it.

This person will ultimately affect your grades. This is a great opportunity to have a positive interaction with them. The more your teachers are involved in teaching you personally, the better your ability to master your subject will be.

If, on the other hand, when you are told that something is wrong, you are leaving privately and asking about the general Internet community, there is a risk that they will tell you that you are right and you will find yourself in a false sense of superiority over yours teachers who will be very counterproductive.

+5
source share

I do not see anything wrong.

the code works because an instance or class can access private members of other instances of the same class. it is by design.

+1
source share

No, there is no problem with that.

Look, it depends on the viewer's opinion. But for a given context, this code might just be perfect.

In another context, this may not be true. So it depends on how it will be used.

  • Access to a private member directly from another instance is correct (not always desirable, for example, when you subclass), why is it private in the first place. You say, "Hey, this is mine, and I know how to use it."

  • Using the default access modifier for the other two methods, says that your intention is that they should not be used by other classes outside the package.

Probably the only thing I would like to add is to make the class final.

 final class Creature 

If you want to make it inheritable, you probably have to reconsider get / set for the yearOfBirth attribute, but they are the way it is ideal for me.

NOW The most important important thing: you understand what each part of your code does, and how it affects its behavior.

You should not have the code just luck (sorry, I don’t know what the correct expression is for this), but you should know what you do each time you enter something, and how you are going to use it.

0
source share

I see two "problems", although I hesitate to call them errors:

  • You explicitly set the creature's age to 89, and then rewrite that age with the uninitialized default value (!) Of the creature d. If this is what you intend to do, then fine, but at least you spend a few cycles to set the value that you intend to throw out later.

  • You might be breaking JavaBeans naming conventions.

To explain the last point: many Java libraries and frameworks (especially JSPs) rely on JavaBeans to process an object as a component. I did not go deep into the actual mechanisms used, but from what I read, it relies on the Introspection of the JavaBeans class to determine the properties and types of these properties. By overloading the setYearOfBirth () setter to accept both int and Creature, you can reset Introspection. (See here for a decent introduction to JavaBeans.)

It doesn’t really matter - it’s quite possible that you will not use this class as a JavaBean, and if you do it trivially to reorganize it to work. But your teachers would probably prefer a little cleaner, like the following:

 class Creature { private int yearOfBirth=10; public void setYearOfBirth(int year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } public static void main(String args[]) { Creature c = new Creature(); c.setYearOfBirth(89); Creature d = new Creature(); c.setYearOfBirth(d.getYearOfBirth()); System.out.println(c.getYearOfBirth()); } } 

Now all your access to yearOfBirth comes through the public getter methods, which help to encapsulate and prevent code breaking if your main method moves to another class. (As Greg D. correctly pointed out.)

In addition, it has the added benefit that the purpose of your code becomes clear, which becomes more and more important when you start writing code for others to maintain and change.

0
source share

Wrong, because you are accessing the private member (you declared private int yearOfBirth ) of another object, although the class type is the same. Instead, you should use the public recipient that you defined: yearOfBirth = other.getYearOfBirth()

-2
source share

yearofBirth is a private int. So calling other.yearOfBirth supposed to fail ...

-2
source share

All Articles