问题
I'm new to Java and I have to create a value object, (maybe it's called mapped object in Java) but my code doesn't seem to work, here is the value object:
package ....;
public class User {
private int id;
private int uid;
private String name;
public User()
{
// do something here
}
}
and I assign a new value object like this:
public boolean some_function()
{
User u = new User();
return true; // got a breakpoint here
}
So if I comment out "User u = new User();" I will go to the breakpoint but if I keep it like above it will just stop running.
On a side note, I keep both the files in the same folder so eclipse doesn't import the file, is this correct or should I import it?
EDIT:
After some time I found out that I had to import the file manually, I thought I tried that but apparently I didn't.
回答1:
Dennis, if the code as you posted it is the exact code you're running, then this makes no sense -- the "User u = new User();" call would return you a new User object without any issues, since your constructor is empty.
To demonstrate that to yourself, change your constructor to:
public User() {
System.out.println("I'm inside the User constructor!");
}
and call your some_function() function again. You should see that line printed out to your console.
Given what you're reporting and the code you're showing, I suspect that the class that contains some_function() isn't "seeing" the User class -- you're importing some other User class rather than the one you created. Are the two classes -- the User class and the class which contains some_function() -- in the same package? If not, what import statement at the top of the some_function()-containing class is handling the import of your User class?
回答2:
Sure you don't have an infinite loop in your User() constructor?
回答3:
Put some code into the constructor, for example
id = 99;
set a break point there.
I don't understand what you mean about importing into Eclipse - I have all my code in Eclipse - however I suspect that your application is not correctly seeing the User class. Maybe you are even getting a compilation error. Create your packages and classes in Eclipse, let it sort out the directories for you.
Show us the whole app class , including the import of User.
回答4:
Put the breakpoint on User u = new User();
and step into the constructor to see what it's doing.
来源:https://stackoverflow.com/questions/1783836/java-value-object