• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused newb cannot pass string between classes  RSS feed

 
Darren Estcourt
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so, I am a newb to Java and self taught. I want to pass a String from my "ManagerName" class into my "DataBase" class. From what I have researched, I need to use encapsulation / getters and setters, to do this. However, after searching online for a LONG time, I cannot get my String to appear in my database, I only get the default "Null" when writing to my database.. This might seem odd, but I find it harder to understand how to pass a string to another class, than I do to write a small sql database and connect to it via jdbc etc.

Now then, I have also read that instead of using getters and setters via methods, I can also do this via a constructor. But I am puzzled as to which is "better". I would be most grateful, if anyone can point out what I'm missing

Two classes concerned are as follows:


-----------------------------------------------------------------------------------------------------------------------------------------------------






 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To "pass a string to a class", you could create an object, then using the object reference, call the setter on that object reference.   Or you can pass the string in with a constructor.

You have two mistakes.  One, you create two objects.

Now you have two object references, one in retrieveTheFirstName and one in retrieveTheSecondName.  One holds the first name only and the other, the surname only.

Second, you never call the method that posts to the database: databaseMethod().

So your code might look something like this:

But before you do that, you really need to get the first name and surname out of the Database class.  Create a Person class to hold the name, then pass an object reference of that class.
 
Darren Estcourt
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er.......I have a main method in a seperate class, which is where I call my databaseMethod()

Also, I am not trying to retrieve the firstname and surname from my databaseMethod, I am trying to pass both names FROM ManagerName() TO my databaseMethod()

I am really grateful for any help with this
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been back to your code and broken the worst offenders amongst the lines too long and removed some excess empty lines which don't aid legibility.
 
Darren Estcourt
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops - sorry I am new here, I didn't realise.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darren Estcourt wrote:Er.......I have a main method in a seperate class, which is where I call my databaseMethod()

Also, I am not trying to retrieve the firstname and surname from my databaseMethod, I am trying to pass both names FROM ManagerName() TO my databaseMethod()

Understood.  But this code:

...makes me wonder if you think that retrieveTheFirstName.setFirstName(firstName) sets the first name into the Database class or something, and that it persists there somehow.  This is not the case.  In the above code you have two objects, one with just the first name set and the other with just the surname set.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!