• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems attaching a method in a class to an object  RSS feed

 
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having problems attaching my method in student: studentIsDoing() to my object st3 what am I missing?











 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

What problem are you having exactly? An error? Post the stack trace. Not doing what you expected? Tell us what you think it should do.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks happy to be here. I know I am a noob but hopefully I will get better I can´t call the method in my main.
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, you don't have a main method. You need to write one.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a main method and where does it go?
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever Java executes, it looks for a method that has this signature:

In your case, you would just instantiate an object of type "student", then call the "studentIsDoing()" method on it.

BTW, your class name "student" should start with an uppercase letter: Student.
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main() method can go anywhere a method can go. I usually see it near the top of the class, just after the declarations, or at the very bottom.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:The main() method can go anywhere a method can go. I usually see it near the top of the class, just after the declarations, or at the very bottom.

But ideally a class that starts a program should contain nothing except the main method.
Main is a pain
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking about that too but can´t figure it out

Like this?


Student st = new Student(studentIsDoing());
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe create a new class to deal with the method but that doesnt make sense
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New code:

Now I can call the method in main but I need to have a different class object execute the method I need st3 to display a random behavor or any student I call on for that manner









 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to do is to create a Behavior field in the Student class.

Then, your method could look like:
I don't like the name studentIsDoing(). For me, Behavior should not be concerned who or what behaves.
It could be a student, it could be a dog or a robot...

Also, you should not put spaces in student's name.
Student class should not be responsible for formatting your output.
The name of Zack is "Zack", not "Zack ".
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I certainly agree about changing the name of my method it fits better. Thank you for the advice about using spaces you are a true guru. I appreciate it. Are you saying I should erase the behavior class and just add a field of behavior? Haha I was not having a good time doing this till I got help and that makes it all worthwhile communicating and growing
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross Gerard wrote:I certainly agree about changing the name of my method it fits better. Thank you for the advice about using spaces you are a true guru. I appreciate it. Are you saying I should erase the behavior class and just add a field of behavior?

No, Don't delete the class. What will you store in the field if you don't have the class?

You need something like:
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wasn´t sure what you are saying in your last message? Can you expaing why I need to create a private variable Behavior behavior likek you suggested?

Okay, have this now but why can´t I call this in main? System.out.println(st3.firstName + b1.randomBehavior());





 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the more surprising things I learned when I was first programming is that a class can be a type, just like String. In fact, java.lang.String is a class, just like Behavior is a class. So when you write:

..."behavior" is just a string, not a type Behavior. That's why you need:

The "private" is because fields are usually private for encapsulation (so that other classes can't access them directly).

Just a note about your code: it should be properly indented. This is something beginning programs forget to do and it's really important. For instance, your Student class should look like this:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!