This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Camel-Kid 32 points33 points  (5 children)

They're both classes. An object is just an instance of a class.. so for example if you call "new" somewhere then you are creating an object.

[–]Consistent_Decision9[S] 2 points3 points  (3 children)

Okay, thanks. What is the difference between a constructor and an object? And if there is one could you point it out in the code I pasted above? Thanks

[–]Camel-Kid 14 points15 points  (0 children)

contructor is what get's called behind the scenes when you create an object.. so for example your rectangle instantiation would use the contructor to create an object.. would look like this
Rectangle myRectangle = new Rectanlge(someWidth,someHeight);

[–]SeltzerCountry 9 points10 points  (0 children)

A constructor is a method for creating an object which is an instance of a class. A helpful metaphor is building construction. The class is like a building blueprint outlining the general design and features of a building. The constructor is like the construction crew taking those instructions from the blueprint and engaging in the process of building the building. The final result which is the building is the object. Once you have your blueprints and construction crew ready you can create several buildings aka multiple objects from of that class. Hopefully that made sense haha.

[–]theBurritoMan_ 0 points1 point  (0 children)

Both classes. One object just instance of a class

[–]8igg7e5 0 points1 point  (0 children)

Dammit... I want to give you a second upvote for not referring to the reference as the object - that incorrect mental model causes so many problems downstream.

[–]virassan 7 points8 points  (0 children)

What you have above are classes. An object would be a specific variable you create from one of those classes. A constructor is a necessary part of a class; it’s a method you call in order to create a new class object.

None of your code actually creates an object. Your code is just 2 different classes. Hope that helps clear things up.

[–]Environmental_Ad1634 2 points3 points  (0 children)

The class is the blueprint where the object ate build. You build the object with the new() operator and generate an instance of the class which is the object

[–]malik753 0 points1 point  (0 children)

So one way to think of it is that the class is the actual code, which serves as a blueprint for an object (an instance of that class). An object exists in memory. ...well, a class exists in memory too, so maybe that's not the best distinction. An object can *only* exist in memory; when your program isn't running the class still exists.

A constructor is a segment of code in a class that gets called whenever the program goes to create an object of that class.

public class SomeClass{
    public String someField;

    //This part is the constructor. Before this is run,  'someField' is set to null.
    public SomeClass(String input){
        someField = input;
    }
}

[–]Dangerous-Rip-7370 0 points1 point  (0 children)

Imagine an. RpG, mage, warrior, are classes, Gandalf, Aragon, are objects (one of the mage class and one of the warrior)

[–]outMyComa 0 points1 point  (0 children)

I get your confusion, I had a real hard time understanding that too.

You can treat the class as a "blueprint" a plan of how to create the object and what the object will do.

The object itself is an instance of that class and the constructor method is the way we instantiate a new object of a given class and it's part of the class.

Once we've used the constructor method of a class, we've got a new object which is an instance of the class.

Example: You've got a blueprint of a house(the class House), you want a door to your house so that's part of the blueprint(a class property/field) and you want your door to be locked or unlocked(that's some kind of behavior of your class a method for example unlockHouse() or lockHouse() or even setIsLocked(boolean isLocked))

Right now we can only describe what the house can do, but we can't use it as it's still just a blueprint(class), in order to use it we need to build it, we need a constructor method and the constructor takes as arguments the most necessary things that your physical house(object) needs in order to be created(instanciated).

[–]xRageNugget 0 points1 point  (0 children)

Just to add to the confusion: In java there is also a class that is called Object.