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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:36:40+00:00 2026-06-01T22:36:40+00:00

Despite Java tutorials, Wikipedia searches, stackoverflow trolling, and hours of reading code samples, constructors

  • 0

Despite Java tutorials, Wikipedia searches, stackoverflow trolling, and hours of reading code samples, constructors still confuse the crap out of me. I’ve got three related questions that I’ve been trying to answer to help ME understand constructors a little better.

First, I’ve been under the impression that constructors need to be named the same as their classes. Consider:

public class Money {
    public Money(long l) {
        this.value = l;
    }

    public Money(String s) {
        this.value = toLong(s);
    }

    public long getLong() {
        return this.value;
    }

    public String getString() {
        return toString(this.value);
    }
} 

I see this as four constructors…correct? So it appears that constructors not named the same as the class which contains them allowable. Can someone confirm that?

Second, I seem to have a block against understanding the set and get methods. Consider:

 public class GetSetSample {
     public int getFoo()  {
 return int Foo;
 }
 public void setFoo(int fooValue) {
 int Foo = fooValue;
 } 
} 

Why can’t I just do this:

public class getFoo(int fooValue){
     foo=fooValue;
}

and use foo = getFoo(12) from some other class/method?

The third question is a little more esoteric, but will help me conceive of the bigger picture…which is my learning style, and conducive to my ability to trace program flow when debugging. The get and set methods suggest a “to” and “from” relationship to me. e.g., Passing a value “to” a constructor, receiving the result “from” the get method. It seems to me though that the “to” and “from” will change depending on your perspective. I think that any setMethod is setting parameters for an object, even though the variable comes FROM another class or method, and the GetMethod is getting the resulting object (say, this.foo) with the appropriately set parameter. No matter where the get or set is used, in a main method or a standalone class with a single constructor, ‘set’ is always associated with sending a parameter and get is always associated with receiving an object with that parameter. Is that a good understanding? or am I missing a vital part?

  • 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-01T22:36:42+00:00Added an answer on June 1, 2026 at 10:36 pm

    Question 1:

    I see this as four constructors…correct?

    No, that class has two constructors and two methods. (getLong and getString are the methods.)

    Question 2:

    Why can’t I just do this:

    public class getFoo(int fooValue){
        foo=fooValue;
    }
    

    Well, that’s trying to declare a class with parameters, and also you’re setting a value in a get method, which would be extremely weird. It’s not clear what you’re trying to achieve here, but that code is thoroughly invalid.

    Question 3:

    The get and set methods suggest a "to" and "from" relationship to me.

    Well it’s not really a relationship IMO. A relationship suggests something longer term than either of these methods. A setter typically changes the state of an object in some way, and a getter typically just returns some aspect of the state of an object. It’s not really clear what the rest of your explanation meant, because you’re playing somewhat fast and loose with terminology. For example: "get is always associated with receiving an object with that parameter" doesn’t really make sense to me. Objects don’t have parameters, methods/constructors do – and getters can fetch primitive values or references…

    I suspect you would benefit from reading the "Classes" part of the Java tutorial, which talks about constructors and methods.

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

Sidebar

Related Questions

Despite me reading wikipedia and such, I still don't really understand what Thread Safety
Many Java Apps don't use anti-aliased fonts by default, despite the capability of Swing
Despite being a Project Euler program, the following code doesn't actually concern it much.
Basically I'm using Flash to connect to a Java server. Despite my Java application
In Java, Hibernate is king in terms of automating code/DB synchronization. And I'm aware
despite some tips, I'm still getting this one wrong. I end up with one
Despite the upcoming java 7 standard fork/join framework, I am building some helper method
With the following code, I get #<CompilerException java.lang.UnsupportedOperationException: Can only recur from tail position
Despite primarily being a windows user, I am a huge fan of rsync. Now,
Despite an earlier question ( asked here ), our project is constrained to using

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.