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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:58:44+00:00 2026-06-15T09:58:44+00:00

In a class, you can create fields within them, as int bar in the

  • 0

In a class, you can create fields within them, as int bar in the following example.

Class foo{
    int bar;
    foo(int bar){
         bar = bar;
    }

    int getBar() {
         return bar;
    }   

    void setBar(int bar) {
         bar = bar;
    }
}
  1. Every time I create a new foo object in another class, will that particular object (instance of foo) have a bar property that when changed with the setBar(), only impacts that instance and no other instances?

  2. I often see people create getters and setters for properties like bar in the above example. If I feel lazy and I’m just writing code for myself/fun, can I just modify the bar property of any instance of this class by accessing the property FooInstance1.bar = 22; instead of having to write a setter and it will have the same effect as in question 1 (just the instance is changed)?

  3. How does the Java compiler know what method is a constructor? By the fact that it has the same name as the class? Or by the fact that one does not specify a return value in the function header? Perhaps it doesn’t even matter what the compiler thinks is a constructor (no syntactic difference between regular function), it could be just a semantic thing that people use to differentiate the meaning of functions.

EDIT: I’m having a hard time selecting a best answer. I learned something new from everyone. Thanks!

  • 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-15T09:58:44+00:00Added an answer on June 15, 2026 at 9:58 am
    1. Yes.
    2. Yes, but it’s not encouraged. With OO, you’re trying to hide information about your instance variables, and provide them access on an as-needed basis. Also, setting the variable to public makes it susceptible to unintentional writes or unprivileged reads.
    3. Java recognizes every method by its signature. A signature is composed of its name and it’s argument types. For example, the main method has a signature of main(String[]). Constructors are identified by them having the same case-sensitive name as the class.

    As an aside, you may want to use this.bar = bar instead, or rename your incoming parameter entirely. Inside of the constructor, the scope of the bar variable is local to what was passed in, so your instance level bar hasn’t been assigned.

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

Sidebar

Related Questions

I want to create properties in class. Where can I create them in attributes
How can I create a singleton class in Objective C?
How can I create a new Exception different from the pre-made types? public class
I'm trying to create a class that can instantiate arrays at runtime by giving
If I create a PolyModel class, such as Person, can I create an Expando
I'm trying to create a C++ class which can work as a holder for
Suppose I am designing a class that can handle any database technology to create
I'm trying to figure out the best way to create a class that can
I would like to create a class whose methods can be called from multiple
Can we able to create two instance of log4j in class one to write

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.