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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:33:03+00:00 2026-05-28T01:33:03+00:00

If I declare a variable to be private, am I able to re-declare it

  • 0

If I declare a variable to be private, am I able to re-declare it in another method?

This is key because I used Netbeans to generate my GUI code, and it uses the same names for variables every time. Is there any way that I can avoid having to change every variable?

ADDITION/EDIT: Does this also apply for the methods themselves? How about objects?

  • 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-05-28T01:33:04+00:00Added an answer on May 28, 2026 at 1:33 am

    The local variables inside a method can not be declared with visibility modifiers (public, private, protected or default), only the attributes of a class can use those modifiers.

    You can reuse the same variable names across different methods, it won’t cause conflicts. It’s a good practice to name the local variables in a method with different names from those of the attributes of the class. To make myself clear:

    public class Test {
    
        private String attribute1; // these are the attributes
        private String attribute2;
    
        public void method1() {
            String localVariable1; // variables local to method1
            String localVariable2;
        }
    
        public void method2() {
            String localVariable1; // it's ok to reuse local variable names
            String localVariable2; // but you shouldn't name them attribute1
                                   // or attribute2, like the attributes
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's start with another behavior: even if you declare a method/variable as private, another
I have a class. In this class I declare a a private variable private
I noticed some people declare a private variable and then a public variable with
I declare a variable as private in the body of the class and set
// declare a class with private variable int * _a // declare a function
how can declare a variable of type <div> in asp.net <div id=b runat=server/> Code
This code: public class MyActivity extends Activity { private final boolean logging = getResources().getBoolean(R.bool.logging);
I declared a private variable vector<SomeClass> theVector; someplace inside my SomeClass class. Why can't
It's possible to declare variables with the below structure in C++ private: public: protected:
Why is it bad practice to declare variables on one line? e.g. private String

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.