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 8597837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:00:31+00:00 2026-06-12T01:00:31+00:00

Given the following code, how can I use myInt in private void jButton1 from

  • 0

Given the following code, how can I use myInt in private void jButton1 from private void jButton2?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int myInt = (Integer)jSpinner1.getValue();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int b = 0;
    int c = myInt;
    do {
        try {
            Object newInstance = Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/rk","root","root");
            PreparedStatement ps = (PreparedStatement) con.prepareStatement("INSERT INTO factors VALUES(?,?,?,?,?,?)");
            ps.setString(1,jTextField7.getText());
            ps.setString(2,jTextField8.getText());
            ps.setString(3,jTextField9.getText());
            ps.setString(4,jTextField10.getText());
            ps.setString(5,jTextField11.getText());
            ps.setString(6,jTextField12.getText());

            ps.executeUpdate();

            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField10.setText("");
            jTextField11.setText("");
            jTextField12.setText("");
        }
        catch (Exception e) {
            System.out.println(e);
        }

        b++;
    } while(b < my);
}
  • 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-12T01:00:33+00:00Added an answer on June 12, 2026 at 1:00 am

    Variables are tied to a Variable Scope. In this case, myInt is declared in one method scope and you are then trying to access it from a different method scope. This is not possible.

    To go around it, you will have to elevate the myInt variable to a Global (Class) variable, like so:

    private int myInt;
    ...
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.myInt = (Integer)jSpinner1.getValue();
    }
    ...
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       int b = 0;
       int c= this.myInt;
    ...
    

    The will mean that anything declared within the class scope will have access to this variable, meaning that your variable is now accessible from all your methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code below, how can I have it modified such that it
Given the following code, is there a way I can call class A's version
The following code is a testing program. Why i can't use A(int a), in
I was given the following code to use video in a canvas, but I
Given the following code which I can't get to compile. template < typename OT,
given the following code: import ctypes ip=192.168.1.1 thisdll = ctypes.cdll['aDLL'] thisdll.functionThatExpectsAnIP(ip) how can I
Given the following code: typedef struct IntElement { struct IntElement *next; // use struct
I use the following code to render a line, given the coordinates in data
The following code gives a NullReferenceException since XPathSelectElement can't navigate through the XPath expression
The following code gives digits whose name is shorter than their value. I can't

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.