Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Accessing variables in public void Method from private void method within same Class

  1. #1
    Junior Member
    Join Date
    Apr 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Accessing variables in public void Method from private void method within same Class

    I am new java programmer (experienced with other languages), and I need a sample code snippet illustrating how to access variables in a public Method from a private Method both in the same Class. Defining the variables in the public Method at the Class level (making the variables accessible globally within the Class) return static values.
    Example of variable assignments in public Method:

    double RS = avgUp / avgDown;
    double RSI = 100.0 - (100.0 / (1.0 + RS));
    rsiseries.setDouble(index, Values.RSI, RSI);

    Double rsiFastMA = rsiseries.sma(index, fastPath, Values.RSI);
    Double rsiSlowMA = rsiseries.ema(index, slowPath, Values.RSI);

    Need to access RSI, rsiFastMA and rsiSlowMA in the public Method from a private Method both in the same Class.

    Any suggestions on how to accomplish this will be greatly appreciated.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Accessing variables in public void Method from private void method within same Class

    Variables declared in a method are local to that method and can not be accessed outside of the method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing variables in public void Method from private void method within same Class

    Quote Originally Posted by Norm View Post
    Variables declared in a method are local to that method and can not be accessed outside of the method.
    Thanks for the response. Researching the question at numerous java sites, there appears to be a way to do this. However, its too esoteric for me as a new java programmer to understand. I'll look at changing the program logic to work-around this issue.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Accessing variables in public void Method from private void method within same Class

    Can you post link to site that shows how to do it?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Calling A Public Static Void Method from Main
    By Clubs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 27th, 2013, 02:29 AM
  2. Replies: 4
    Last Post: June 6th, 2013, 10:30 AM
  3. Assign a value in a void method
    By Freezer Boy in forum Object Oriented Programming
    Replies: 4
    Last Post: December 12th, 2012, 11:13 AM
  4. Why my void run method cannot be execute?
    By 90th century in forum Threads
    Replies: 2
    Last Post: March 30th, 2012, 12:09 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM