• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having trouble invoking methods from another class?  RSS feed

 
Jeffrey Gutierrez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the subclass, everything checks out,




now when I try to invoke the methods here to generate a list of random characters, the Class.method(); syntax doesn't work, culprits are on line 21 and 22, the forloop is red and so is the method call


 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure about exact error message you get, but it seems these two classes are in different packages.
 
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
Your classes are in different packages.

You need to add some import statements.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And your for loop is missing an i.
 
Jeffrey Gutierrez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest that using Math#random like that is a very error‑prone way to get those letters. Have you considered using an array of letters and using Random#nextInt?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeffrey Gutierrez wrote:This is the subclass, everything checks out

For the sake of clarity: RandomClass is not a subclass of RandomProgram class (I presume this is what you meant), in fact, RandomClass is a subclass of class Object.
You could read about it here < link
 
Jeffrey Gutierrez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Jeffrey Gutierrez wrote:This is the subclass, everything checks out

For the sake of clarity: RandomClass is not a subclass of RandomProgram class (I presume this is what you meant), in fact, RandomClass is a subclass of class Object.
You could read about it here < link
I understand the concept that classes in the java api are inherited, but what about classes that you make from scratch? can you make them inheritable? Say I create 7 classes and I want them all working and communicating with eachother, is there a specific way of putting them together?

forgive me from being vague... I promise I'll get better at that <3

Campbell Ritchie wrote:I would suggest that using Math#random like that is a very error‑prone way to get those letters. Have you considered using an array of letters and using Random#nextInt?


this is the perfect opportunity for me to learn how to do that as I just started my first lesson on arrays today, can you explain how to do it? and also, why are you using Math#Random and not Math.Random?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeffrey Gutierrez wrote:I understand the concept that classes in the java api are inherited, but what about classes that you make from scratch? can you make them inheritable? Say I create 7 classes and I want them all working and communicating with eachother, is there a specific way of putting them together?

I'm not sure I fully understand you question.
- Yes, you can inherit attributes (variables), operations (methods) from your classes created from scratch (it does "extend").

- To make classes working together and communicating with each other - this situation probably in a best way describes UML term "coupling". Degree at which classes are interconnected with each other.
Communication happens by sending messages (invoking methods) usually on particular instances of the class object.

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I create 7 classes and I want them all working and communicating with each other, is there a specific way of putting them together?


There isn't a specific way, but one way is to pass some variable around to the methods:

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find a long discussion about ”random“ numbers here. I hope you will find it helpful
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!