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.