Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7770697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:28:17+00:00 2026-06-01T16:28:17+00:00

So I’m trying to figure out how can I have 3 classes call one

  • 0

So I’m trying to figure out how can I have 3 classes call one another.

this is the main class.

public class TestStudent {
    public static void main(String[] args) {
        myStudent mystudent_obj = new myStudent();
        mystudent_obj.show_grades();
        mystudent_obj.change_grades();
        mystudent_obj.show_grades();
    }
}

This is the 2nd class that’s being called in the class above;
The 2nd class call another 3rd class and try to manipulate it
using two functions. Function show_grades just print out the variables in the 3rd class
and function change_grade try to change the variables in the 3rd class.

public class myStudent {
    public void show_grades(){
        Student student_obj = new Student();
        System.out.println(student_obj.studGrade);
        System.out.println(student_obj.studID);
    }

    public void change_grades(){
        Student student_obj = new Student();
        student_obj.studGrade='V';
        student_obj.studID=10;
    }
}

This the 3rd call, which only has two variables.

public class Student {
    public int studID = 0;
    public char studGrade = 'F';
}

when I run the program it runs without errors and I get an output of:

F
0
F
0

however, I can see that the function show_grades work and it does display the grades, but
the function change_grades does not change the grades:

The end results, should be something like this

F
0
V
10

because the change grade function, should have changed those variables… so what’s going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:28:18+00:00Added an answer on June 1, 2026 at 4:28 pm

    In your myStudent class you are creating a new instance of Student in each method, meaning that each method has a local variable of class Student. When you call show_grades the second time, a new instance is created, with the default values of 0 and F.

    If you create a variable and use that instead, your change grades will change the variables of the instance variable instead of a local variable in each method. This is due to scoping in programming, which you can read more about at Wikipedia.

    public class myStudent {
        private Student student_obj = new Student();
    
        public void show_grades() {
           System.out.println(student_obj.studGrade);
           System.out.println(student_obj.studID);
        }
    
        public void change_grades(){
            student_obj.studGrade='V';
            student_obj.studID=10;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.