Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

variable accesibility

  Asked By: Hisham    Date: Feb 08    Category: Java    Views: 675
  

when i call a method... say for
example:float meth(){<br>return floatVar;}from
inside the class its in (say class1), it has access to
floatVar, a variable in this class... but when I call the
method from another class (say class2), it doesn't have
access to this variable floatVar from class1. How can I
call the method from class2 and have access to the
class1 variables?

Share: 

 

10 Answers Found

 
Answer #1    Answered By: Geraldine Perkins     Answered On: Feb 08

Try: public float  meth(){ return
floatVar; }

 
Answer #2    Answered By: Corey Jones     Answered On: Feb 08

yes, i tried adding the keyword "public" to the method, but alas, i did
nothing...

 
Answer #3    Answered By: Troy Kelley     Answered On: Feb 08

What do you mean it did nothing. Can you send me a snippet of code
showing how you are trying to use that method  (i.e. the code which is
calling that method)?

 
Answer #4    Answered By: Gorkem Yilmaz     Answered On: Feb 08

here is some of my code... is there a way to change it so that the
variable being returned as 0.0 can take the value of oneNum, like the
variable test? thanks

public class  treeCounter extends JApplet implements ActionListener {
float oneNum, test;
public void init(){
gV();
test = gV();
System.out.println(test); // returned as 3.0
f = new SampleFrame(output);
}
}

class SampleFrame extends Frame{
// a few variable  declarations
SampleFrame(String title){
// other code in here
Main main = new Main();
main.show();
}
}

class Main extends JFrame{
// variable declarations
public Main(){
treeCounter dp = new treeCounter();
oNum = dp.gV;
System.out.println(oNum); // returned as 0.0
// more code
}
}

 
Answer #5    Answered By: May Hansen     Answered On: Feb 08

In treeCounter, do you have a method  gV() or a variable  gV? If it is a
method, then your code would be:

oNum = dp.gV();

If it is a variable, then you can declare the variable as public in your
treeCounter and do this:

oNum = db.gV;

However, the second example is not good object-oriented programming as
it breaks encapsulation.

You did not provide the code where gV the method or variable can be
found, so I am not sure how to interpret this.

 
Answer #6    Answered By: Abana Cohen     Answered On: Feb 08

Variables which are declared within a method  have
a scope local to that method.<br><br>If you want
variables accessable from method to method, you need to
declare them in the class.<br><br>For
example:<br><br>public class  myClass{<br>private int
firstVariable;<br><br><br>public method1(){<br> firstVariable = 10;<br>
System.out.println("FirstVariable: =" + firstVariable);<br>}<br><br>public
method2(){<br> int localVariable = firstVariable;<br>
firstVariable = firstVariable + localVariable;<br>
System.out.println("FirstVariable has Changed: + firstVariable);<br>
System.out.println("LocalVariable now equals : " +
localVariable);<br>}<br><br>}<br><br>While one can do this, it is not a very
good
programming practice. variables  which you want to share
access to should be passed into a method and returned if
they need to be changed outside the scope of that
method.<br><br>If you are changing too many "instance" variables,
it's time to look at your design. Doing this causes A
LOT of problems in maintance.

 
Answer #7    Answered By: Lu Fischer     Answered On: Feb 08


yes, my variable  is declared in the class, and can be accessed by other methods
in the class, but that's not the issue, I want access  to it when the method  is
called from ANOTHER class.

 
Answer #8    Answered By: Alfonso Martin     Answered On: Feb 08

Did you see my post regarding making your method  public?

You could also mark your variable  as public, but this goes against one
of the primary tenets of object-oriented programming, namely
encapsulation.

 
Answer #9    Answered By: Amir Shaikh     Answered On: Feb 08

Why do you have to be so complicated? You're gonna confuse the poor
guy.

 
Answer #10    Answered By: Abriana Rossi     Answered On: Feb 08

I've been talking with SmallTalk programmers.... I've been
corrupted!!

 
Didn't find what you were looking for? Find more on variable accesibility Or get search suggestion and latest updates.




Tagged: