• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need a better solution  RSS feed

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I have 2 class A and B. I passed values to B constructor n done some thing and sent values back to class A method. Below is the code. i think there is much more better solution than this. Bcauz i am a noob.
class A


class B




Thanks and Regards
Adeeb>
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adeeb alexander wrote:Hi all.
I have 2 class A and B. I passed values to B constructor n done some thing and sent values back to class A method. Below is the code. i think there is much more better solution than this. Bcauz i am a noob.


Perhaps it's me, but I'm not sure I understand just what your problem is. What is working/not working with this code? What in particular bugs you? And also, just what is this code supposed to do.

Also, just to be sure, you do know that this is not a constructor but rather a dangerously named method, correct?


edit: ah, you've posted the second class, AndTest. One point to make, if this is truly a test class, then it is my impression that you shouldn't have the class you are testing, And, call methods from the tester, TestAnd, that the first class should have no knowledge of the second.

Much luck! Pete
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to fix your problem with And calling the method from the test class is to give And two List<String> private variables complete, say cols and vals, with public getter methods, and to have your And constructor (if you give it one) and its methods use these class fields and not re-declare them elsewhere. Then you can have the test program retrieve and use the two Lists via And's getter methods.

And, again please re-name the pseudo-constructor method that you currently call And. Either that or make it a true constructor.

Luck!

Pete
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much stein. Is it necessary to use a constructor or just we can write prog without it??
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adeeb alexander wrote:Thank you so much stein. Is it necessary to use a constructor or just we can write prog without it??


Well, as far as I know, all classes must have a constructor, but if you don't provide an explicit constructor, Java will implicitly provide a default one. So your And class doesn't need a constructor, and it's fine to use the And method, but again, you will want to rename the method since as it is written with the pseudo-constructor, it will confuse many who read the code. Also method names should not be capitalized.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us what the thread is about, and avoid misspellings like "n" and "bcauz".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!