• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Need a better solution

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always tell us what the thread is about, and avoid misspellings like "n" and "bcauz".
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic