Advertisement

Constructor questions

Started by June 12, 2017 05:22 AM
3 comments, last by Xer0botXer0 7 years, 5 months ago

Hi guys,

So I've definitely made a bit of progress understanding basic java syntax, so far I get that each java project has to have a main class, please correct me where I'm wrong, the main class is always run first, any code outside of the main class that is not referenced or called in any manner within the main class will not run.

A constructor is a block of code used to initialize an object of a class, it has to be the same name as the class.

Can a constructor be called from anywhere as long as the class it's using as the constructors name is public ?

When a new object of a class is created, depending on the parameters it will decide which constructor to use because constructors can be overloaded.

so if I have these two constructors


public Players(String Username, Int Pass) [..catch exceptions]
{
usn = Username;
pass = Pass;
}

public Players(int ID, String Username, Int Pass) [..catch exceptions]
{
user_ID = ID;
usn = Username;
pass = Pass;
}

Players Player = new Players(x, "John", "Doe");

In the above example I've got two constructors, when I create a new object of class Players called Player it uses the second constructor due to the amount of parameters used and their data types being the same as the second constructor.

Now I'm a bit confused as where these parameter values are being passed from.

So my question is, when a constructor is created it is given parameters if created manually, what are these parameters for ?

I see that within the constructor I am now assigning these parameter values to variables within the constructor.

But only when i create an object am I assigning actual values to the variables..

As you can see I am confused with something here, there's something I'm missing.

Now I'm a bit confused as where these parameter values are being passed from.

So my question is, when a constructor is created it is given parameters if created manually, what are these parameters for ?

I see that within the constructor I am now assigning these parameter values to variables within the constructor.

But only when i create an object am I assigning actual values to the variables..

I think it may be beneficial for you to really differentiate the difference between an object, and a class. A class, in simple terms, is the schematic of the behavior of the soon to be object you write in code. From the code above I can see the class in question supports being initialized into an object by either constructor depending on if two parameters are passed to it's constructor, or three. (as you accurately observed in noting constructor overloading).

Players Player = new Players(x, "John", "Doe");

Here is where the magic happens. Your class in the scope of runtime is transformed from just a class to an Object in memory. Your class defined that two constructors exist, and you initialize the Object with the latter of the two that uses three parameters.

So basically with that above constructor, not to get to technical, but those three arguments you passed from main are automagically copied over (Look up Calling Conventions if you want the juicy details of this stuff, though the JVM may have it's own rules), and essentially in runtime you get this with the object creation

public Players(int ID = x (Whatever value x was), String Username = "John", Int Pass = "Doe" <= This likely will undergo some implicit casting, or give an error)
{
user_ID = ID;
usn = Username;
pass = Pass;
}

Now to answer the earlier question, these parameters are for the members of the object when it is initialized, so that it can actually do something useful (At least in this case). Some objects don't require any data from the user during initialization, and some do. It really just depends on the design requirements.

Advertisement

So basically,

Edit: see below

When I create a class, it's not going to run until an object of it is created.

As you say no memory is allocated for this object until it's created, so the class template/schematic is just there for when an object of it is created.

Each object created from a class is copy of it, so it gets all the code from that class schematic.

This makes more sense, my bad with the parameters lol I thought i said name and surname.

I'm still a bit confused with the parameters.

public Players(int ID = x, String Username = "John", Int Pass = "1234")
{
user_ID = ID;
usn = Username;
pass = Pass;
}

From what I see I'm declaring variables in the Players class parameters and assigning them values, I don't see the significance in them as why cant I just do this

public Players()
{
int user_ID = 8;
String usn = "John";
int pass = 1234;
}

The above obviously gives an error because it's not using the void keyword which represents whether or not a class will return a value.

Im confused with this because Im familiar with GML, where when we want to retrieve a variable value from another instance, (gml instance to java would be a created object in a way) we would do something like this "speed = obj_train.speed;" here i assign the current instances speed with the speed of another instance.

So I dont think it works the same in java where in java we have to use parameters to pass values ?

Thanks MarkyPooch for the aid so far.

Edit:

Can I see parameters as a way to make each object of a class unique ?

Im not sure if Java has switch statements or case statements, but what I mean is say I pass the value 0 to the object, then in the object i check what the value is, if it's 0 then run this code, if it's 1, run that code.

public Players()
{
int user_ID = 8;
String usn = "John";
int pass = 1234;
}

This is completely valid, it's called a default constructor since it requires no input from an end-user. However in that block of code you declared those variables in that constructor. Meaning once you exit the scope of that constructor, those variables will be tossed off the stack, along with their values.

instead use the 'this' keyword to reference the object, and it's internal members.

public Players()
{
this.user_ID = 8;
this.usn = "John";
this.pass = 1234;
}

The point for parameters for constructors is to give an end-user control of the variables of that object upon creation. This can be described as follows:

public class Entry {

public static void main(String[] args) {

Person person1 = new Person(x, "John", "Doe");

Person person2 = new Person(x, "Mary", "Jane");

Person person3 = new Person(x, "Alan", "Richman");

}

}

If you define that class to only have a default constructor, than I couldn't do the above during initialization. As a default constructor, the name "John Doe" is fine. But not every person has the same name, and it saves me some time to do just define some basic characteristics of a person right from the get go by using the constructor.

Though, worth noting. After the object is created, depending on the access qualifier of the member (If they are public), I can do this from a scope that has a valid reference to the object

{

Person person1 = new Person(); //Default constructor

person1.usn = "Mark";

person1.pass = 1234;

//ect.

}

Down the road you should also hear about Accessor Methods. Basically methods that's defined in a class that allow limited r/w capacity to a caller to a private member. But that's down the road.

Does each java file need a main method ? I thought it only needs a main class. I'm not too sure what methods are yet.

It looks like I can write code in this main method and it will run, but code run within the class will not.

This prints "Halo"


package myPackage;


public class Calculator {


public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Halo");
}


}

Where this doesnt


package myPackage;


public class Calculator {


public static void main(String[] args) {
// TODO Auto-generated method stub


}
System.out.println("Halo");
}

This topic is closed to new replies.

Advertisement