Java variable scope question (returning objects and private methods)

E

exquisitus

I'm porting code from C++ to Java. I have functions that have in/out
parameters - i.e. accept references (i.e. pointers and pointers to
pointers). In C++, this allowed me to return more than one variable.

In Java, I'm having to decompose such functions into several smaller
functions. To avoid code bloat (and also reduce typing errors/increase
maintainability of the handcrafted Java code - going forward). I want to
have each of these smaller functions to be merely a "wrapper" function
for a single block of code. Since I don't want this block of code to
show up in the (Java) API, I want to make it a private function (method).

My question then is that I can I then return the private object it
returns?. Here is some pseudocode to illustrate further:

class myClass {
....
public XObject foo_1(...)
{
XObject t = null ;
t = foo_hidden(...) ;
return t ; //Will this object be visible to callee ?
}

private XObject foo_hidden(...)
{
XObject x = null ;

//Some stuff here
return x ;
}

};

If this will not work, could someone please explain why it won't, and
equally importantly, how to "wrap" up a function as I explained - to
avoid having to repeat almost identical code several times in seperate
function calls.

Ta
 
A

Anthony Borla

exquisitus said:
I'm porting code from C++ to Java. I have functions that
have in/out parameters - i.e. accept references (i.e.
pointers and pointers to pointers). In C++, this
allowed me to return more than one variable.

In Java you would return multiple variables via:

* Use of an array [or collection-type object]; you
might pass an array as argument, and update its
elements in the method body

* Redesign of program to have method return
an object which encapsulates the multiple values,
or, as for the array, pass such an object as an
argument and use its setters to update it
In Java, I'm having to decompose such functions into
several smaller functions. To avoid code bloat (and also
reduce typing errors/increase maintainability of the
handcrafted Java code - going forward). I want to
have each of these smaller functions to be merely a
"wrapper" function for a single block of code. Since I don't
want this block of code to show up in the (Java) API, I
want to make it a private function (method).

Ok. So your actual 'work code' is in the body of 'private' methods, and you
provide 'public' methods that act as the class interface, and simply
delegate work to the relevant 'private' method(s).

This seems a perfectly reasonable approach to take since it provides a fair
degree of flexibility. Once you've finalised the class interface you can
then always move the code directly into the body of the relevant 'public'
function(s) [if need be].
My question then is that I can I then return the private
object it returns?. Here is some pseudocode to illustrate
further:

class myClass {
....
public XObject foo_1(...)
{
XObject t = null ;
t = foo_hidden(...) ;
return t ; //Will this object be visible to callee ?
}

private XObject foo_hidden(...)
{
XObject x = null ;

//Some stuff here
return x ;
}

};

If this will not work, could someone please explain why it
won't, and equally importantly, how to "wrap" up a function
as I explained - to avoid having to repeat almost identical code
several times in seperate function calls.

The answer is a resounding: YES, it will work fine. The access specifiers
merely prevent direct *member* access, and have no affect on the return
values of methods. Of course the class of object you are returning has to be
declared so that it is visible wherever it will be used, something that can
be taken largely for granted if all classes are declared at the top-level
[i.e. globally, not within the scope of any other class].

I hope this helps.

Anthony Borla
 
J

John C. Bollinger

exquisitus said:
I'm porting code from C++ to Java.

Just because C++ and Java are object-oriented languages with similar
syntax does not mean that porting code from C++ to Java is easy. There
are some deep differences in how these languages work, and as a result
there are numerous "gotchas" involved in the task.
I have functions that have in/out
parameters - i.e. accept references (i.e. pointers and pointers to
pointers). In C++, this allowed me to return more than one variable.

That's a little vague, and technically speaking it is certainly
inaccurate. You may have had functions or methods that manipulated
their arguments, but that doesn't give you multiple return values. You
in any case can never return a variable -- only a value. Java also
permits methods to manipulate their parameters, and this can be used to
communicate with the invoker, including to give it access to chosen
objects. Because Java does not have pass by reference semantics,
however, assigning a new value to a method parameter has no visible
effect outside the scope of the method, and _that_particular_technique_
for communicating through method arguments with the invoker is not
available.
In Java, I'm having to decompose such functions into several smaller
functions. To avoid code bloat (and also reduce typing errors/increase
maintainability of the handcrafted Java code - going forward). I want to
have each of these smaller functions to be merely a "wrapper" function
for a single block of code. Since I don't want this block of code to
show up in the (Java) API, I want to make it a private function (method).

I think you may want to reconsider your plan: there are almost surely
better ways to accomplish your goal. If you present an example
problematic function / method from you C++ API then we may be able to
offer some advice on how better to reimplement it in Java.
My question then is that I can I then return the private object it
returns?. Here is some pseudocode to illustrate further:

There are no private or local objects in Java, only private and / or
local variables. Moreover, technically speaking you can never return an
object from a method (or pass one as an argument), only references to
objects.
class myClass {
....
public XObject foo_1(...)
{
XObject t = null ;
t = foo_hidden(...) ;
return t ; //Will this object be visible to callee ?
}

Again speaking technically, the object referred to by the reference
returned by this method is neither more nor less visible or accessible
to the invoker than any other object of its class.

[...]
};

If this will not work, could someone please explain why it won't, and
equally importantly, how to "wrap" up a function as I explained - to
avoid having to repeat almost identical code several times in seperate
function calls.

If multiple blocks of identical or near-identical code are appearing in
your program then you have a design problem. I'm afraid I don't
understand how that would need to arise in Java if it was not already
problem in C++.
 
E

exquisitus

John said:
exquisitus said:
I'm porting code from C++ to Java.


Just because C++ and Java are object-oriented languages with similar
syntax does not mean that porting code from C++ to Java is easy. There
are some deep differences in how these languages work, and as a result
there are numerous "gotchas" involved in the task.
I have functions that have in/out
parameters - i.e. accept references (i.e. pointers and pointers to
pointers). In C++, this allowed me to return more than one variable.


That's a little vague, and technically speaking it is certainly
inaccurate. You may have had functions or methods that manipulated
their arguments, but that doesn't give you multiple return values. You
in any case can never return a variable -- only a value. Java also
permits methods to manipulate their parameters, and this can be used to
communicate with the invoker, including to give it access to chosen
objects. Because Java does not have pass by reference semantics,
however, assigning a new value to a method parameter has no visible
effect outside the scope of the method, and _that_particular_technique_
for communicating through method arguments with the invoker is not
available.
In Java, I'm having to decompose such functions into several smaller
functions. To avoid code bloat (and also reduce typing errors/increase
maintainability of the handcrafted Java code - going forward). I want
to have each of these smaller functions to be merely a "wrapper"
function for a single block of code. Since I don't want this block of
code to show up in the (Java) API, I want to make it a private
function (method).


I think you may want to reconsider your plan: there are almost surely
better ways to accomplish your goal. If you present an example
problematic function / method from you C++ API then we may be able to
offer some advice on how better to reimplement it in Java.
My question then is that I can I then return the private object it
returns?. Here is some pseudocode to illustrate further:


There are no private or local objects in Java, only private and / or
local variables. Moreover, technically speaking you can never return an
object from a method (or pass one as an argument), only references to
objects.
class myClass {
....
public XObject foo_1(...)
{
XObject t = null ;
t = foo_hidden(...) ;
return t ; //Will this object be visible to callee ?
}


Again speaking technically, the object referred to by the reference
returned by this method is neither more nor less visible or accessible
to the invoker than any other object of its class.

[...]
};

If this will not work, could someone please explain why it won't, and
equally importantly, how to "wrap" up a function as I explained - to
avoid having to repeat almost identical code several times in seperate
function calls.


If multiple blocks of identical or near-identical code are appearing in
your program then you have a design problem. I'm afraid I don't
understand how that would need to arise in Java if it was not already
problem in C++.

Thanks John, for your response. I suppose in trying to be concise, I
glossed over some of the technical details. It really is (should) be
quite simple. This is what I'm trying to do:

I have a method in C++ declared like this:

int foo( const structA *, structB**, int*, int*, int*, float*, const int) ;

Arguments 2-6 are addresses of variables. This allows me to return 5
values at the same time. I want to do decompose this function into
wrappers like this (C pseudo code given as an example):

structB *foo_1(const structA *, int, enum RESULT_TYPE ) ;
int foo_2(const structA *, int, enum RESULT_TYPE ) ;
int foo_3(const structA *, int, enum RESULT_TYPE ) ;
int foo_4(const structA *, int, enum RESULT_TYPE ) ;
float foo_5(const structA *, int, enum RESULT_TYPE ) ;

Each of these "wrappers" will call a single block of code (with the
appropriate logic to determine output required).

In this case (still using the C/C++ analogy), my 4 functions above) will
be exposed in my new API, and my called, single chunk of code will be
declared/defined as a static function. I was wondering if/how I could do
something similar with Java.

look forward to your response

Many thanks
 
T

Tilman Bohn

In message <[email protected]>,
exquisitus wrote on Tue, 22 Feb 2005 00:28:37 +0000 (UTC):

[...]
I have a method in C++ declared like this:

int foo( const structA *, structB**, int*, int*, int*, float*, const int) ;

Arguments 2-6 are addresses of variables. This allows me to return 5
values at the same time.
[...]

One standard approach to this would be to create a `dumb' wrapper class
for these values and pass that instead. Then within the method body, you
can manipulate its fields to your heart's content through mutators, and
can easily delegate computation of single values to private helpers.

It is not unlikely that you will later find it is more appropriate to
have some amount of the logic pertaining to the data contained by the
wrapper on that wrapper (OO 101, really). When this looks natural, you
should not hesitate to refactor appropriately.
 
J

John C. Bollinger

exquisitus said:
I have a method in C++ declared like this:

int foo( const structA *, structB**, int*, int*, int*, float*, const
int) ;

Arguments 2-6 are addresses of variables. This allows me to return 5
values at the same time. I want to do decompose this function into
wrappers like this (C pseudo code given as an example):

structB *foo_1(const structA *, int, enum RESULT_TYPE ) ;
int foo_2(const structA *, int, enum RESULT_TYPE ) ;
int foo_3(const structA *, int, enum RESULT_TYPE ) ;
int foo_4(const structA *, int, enum RESULT_TYPE ) ;
float foo_5(const structA *, int, enum RESULT_TYPE ) ;

Each of these "wrappers" will call a single block of code (with the
appropriate logic to determine output required).

Yes, I had suspected that that sort of structure was what you had in
mind, but does it make sense? Does the original C++ code do a bunch of
extra computation to be able to determine all five values? Does it only
determine some of them, depending on the arguments? [Yuck!] Or do you
plan to redo the calculation five times when you need all five values in
the Java version?

If all the return values are results of the same computation, then you
should have one method returning as its one result an object that
encompasses all of them. (It need not have each as a separate member --
for instance, perhaps there is a standard conversion by which some
results can be cheaply computed from others.) This would have been
better in C++, too.

If the return values come from different, albeit related, computations,
then each one should have its own method with its own code. Where there
are significant bodies of code in common among these methods then by all
means factor it out into private methods used by the front-end methods.
In this case (still using the C/C++ analogy), my 4 functions above) will
be exposed in my new API, and my called, single chunk of code will be
declared/defined as a static function. I was wondering if/how I could do
something similar with Java.

Java methods can invoke other methods of their own class or of another
class to which they have access, but the keyword "static" means
something different in C++ than it does in Java, IIRC. In Java it means
that that the class member to which it applies is shared by all class
instances; for methods it also means that the method is not virtual (as
opposed to all other non-private methods). Static class members can,
and in fact should, be referenced without use of an instance.

Whether a member is static is orthogonal to whether it is accessible
from any particular piece of code. If you only want a method to be
accessible to other code in the same class, then make it private. If it
should be accessible only to other classes in the same package then
leave it with default access. The access level chosen is what
determines your classes' API.
 
S

sanjay manohar

I'm sure you're aware of this but to simply return many values at once
is easy to emulate - though it might be methodologically dubious! It
does exactly what the C++ code does, i.e. pass a reference to a struct.
But note the criticisms you yourself have raised of this method...

class StructA { int a, int b, int c }

public static void main(String[]s){
StructA s=new StructA();
foo(s);
System.println(s.a+","+s.b+","+s.c);
}

void foo(StructA x){ x.a=1; x.b=2; x.c=3; }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top