Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy basic java question 1

Status
Not open for further replies.

siberian

Programmer
Sep 27, 2003
1,295
US
Haven't touched java in about 5 years, been in the perl world.

What i am trying to do is use one method for my setter and my getter in a class. Here is how I *think* it should look.
<pre>
----
protected int test_var ;
public int test_var(int new_test_var) {
if(!new_test_var){
test_var = new_test_var;
}
return test_var ;
}
----
</pre>

Now obviously this doesn't work, the int type in java doesn't return a boolean. Is there an isInt style call I can use to see if an int was passed into the function.

This is so Java 101, thanks for any solutions.

 
I also tried this :

---
public int test_var(int new_test_var) {
try{
Integer theInteger = new Integer(new_test_var);
test_var = new_test_var ;
}catch (NumberFormatException err)
{
/* who cares, we dont */
}
return test_var;
}
---

but when I do that my get context fails since I am not passing an int in.

Any ideas on how to combine these two functions or is it just a perl thing that happens since perl has no datatyping?
 
Code:
class TestInt
      {
       protected int test_var ;
       public boolean test_var(int new_test_var) 
             {
              System.out.println(&quot;parameter Obtained: &quot;+new_test_var);
              return true; 
             }

       public static void main(String args[])
              {
               TestInt testIntObj = new TestInt();
               testIntObj.test_var('A'); // pass a character literal into function

               byte b = 127; //(should be between -128 and 127) pass a byte literal function
               testIntObj.test_var(b);

               short s = 125; // pass a short literal into function
               testIntObj.test_var(s);    

               if (testIntObj.test_var(s))
                  System.out.println(&quot;accept parameter&quot;);

               // without casting, you can also pass a character, byte or short into a function that requires a integer literal for parameter      
               // This is called promotion
               // reference url [URL unfurl="true"]http://www.absolutejava.com/main-articles/data-type-conversions/[/URL]
               // You cannot pass null or object to test_var, like 
               // test_var(new Object); incorrect
               // test_var(null); incorrect
 
               // you should tell us what you want to do, like count the array length...
              }
      }
 
At first glance I don't see how this works. I'll give it a try this weekend and see where it takes me.

What I am not seeing:

A) It seems like test_var always returns true no matter what you pass to it.
B) test_var always requires an argument so it can only be used for setting, not getting.

I currently have individual set and get methods for each variable in my object, my instinct is to have a single method that does both. This may just not make sense in Java.

I'll muck around and see where this takes me, thanks.
 
It sounds like you want it to set and get... then youll need to take an Integer not an int...
siberians should work the way your looking for, if you modify it to take Integers and test for
Code:
null
instead.
The better way to this is to have two functions named test_var, that way you can pass a int or not.
Code:
private int testVar;
public int testVar(Integer i)
{
  if(i == null)
     return testVar;
  else
     testVar = i.intValue();
  return testVar; //this will most likely be thrown away
}
OR
Code:
public int testVar()
{
   return testVar;
}

public void testVar(int newTestVar)
{
   testVar = newTestVar;
}
 
Ahh, I see, two methods that basically override each other depending on calling context. I like it!

Thanks istreich.

My solution to this problem has been to re-look at how I am doing things and ditching getter/setters entirely allowing my objects to be real black boxes.

This tip will come in immediately handy on some other items though! Thanks.

John-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top