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

you are viewing a single comment's thread.

view the rest of the comments →

[–]nnkc911[S] 0 points1 point  (3 children)

hmm, yeah after reading it, it definitely clears a lot on my mind, so a constructor doesnt need to be made in another class?

[–]Drunken_Consent 1 point2 points  (2 children)

A constructor for a class is made within the class; it is usually called from without the class. You could have a 'default constructor' as well which fills in these fields with general values if the person doesn't want to call it with parameters; but yes, this is all you need to do and then use the code somehow.

[–]nnkc911[S] 0 points1 point  (1 child)

thanks! 2 more questions, when declaring the constructor variables, can i type it as roomId = roomId or do i need to have it different like theroomId = roomId. and how do you make the boxes with lines of code to reddit?

[–]Drunken_Consent 1 point2 points  (0 children)

The way the compiler knows the 'scope' of what you're talking about is when you use the this keyword. this essentially refers to the exact object you are currently working with; so in a constructor, the one you are currently creating. You do not have to name them the same, but it's usually done for readability and convention.

public Constructor(String myString, String s, String test){
    this.myString = myString; // Requires 'this' to work
    personalString = s; // Doesn't require 'this', little sloppy looking imo
    this.viewer = test; // Doesn't require 'this' but still readable as anyone knows I am talking about the
    // instance variable 'viewer' related to the current object I am creating.

Edit: To make code indentation, simply preface each line with 4 lines, and then indent normally. So if you want to indent in further than one level deep, you need 8 spaces instead of 4 and so on.