Jump to content

help with constructors

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
so I've just recently started learning about objects which I'm sure is obvious if you look at the threads I've been posting. What I don't really get is what the point of constructors even is. Aren't they essentially just variables?

say I have this code:

public class Account {

	double balance;

	

	public Account(double initialBalance){

		balance = initialBalance;

	}

	public Account(){

		balance = 0.0;

	}

	

	}

why can't I just write it like this?

public class Account {

	double balance;

	double initialBalance;

	

	initialBalance = balance;

	balance = 0.0;

	

	

}



#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I don't know Java, but objects are not exactly variables. The thing is, an object is usually a collection of variables, stored in memory, each having a unique name. The pointer to the collection of variables, in memory, is stored in the variable you use in accessing the object. Sub-object references within the object are just pointers to new objects, so if you make a copy of the object you're working with, and change one of the sub-objects' variables, you would end up changing that variable for both of the objects' sub-objects. In my personal experience, in JavaScript, to fix this problem, I wrote a DuplicateObject () function, which not only copies the object, but also all the sub-objects, etc.

#3
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
i appreciate your help but that didn't really answer my question. I'm pretty solid on the concept of objects, I just don't understand constructors.

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
This:

public class Account {     
double balance;     
double initialBalance;          
initialBalance = balance;     
balance = 0.0;           
}
won't work because you cannot access variables in this manner in java. Variables are accessed via methods.
Why do you need constructors?
A constructor guarantees initialization of every object. "If a class has a constructor, Java automatically calls the constructor when an object is created...So initialization is guaranteed."

When you say
Account bigmoney = new Account();
or 
Account smallmoney = new Account(10.0);
storage is allocated and the constructor is called. It is guaranteed that the object will be properly initialized before you can get your hands on it. Without a constructor you will have no method to call to build your object.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#5
AfroJack

AfroJack

    Newbie

  • Members
  • PipPip
  • 24 posts
A constructor is used to create an object. For example if you have a student class and the variables inside are GPA and course, the constructor would take in these variables amd then create an object wuih them variables.

Student AfroJack = new Student(4.0, Computer Science)

This just creates an object with them variables set ( if your constructor has a set function )



Sorry I cant post more java code I'm On My Phone

#6
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET

AfroJack said:

A constructor is used to create an object. For example if you have a student class and the variables inside are GPA and course, the constructor would take in these variables amd then create an object wuih them variables.

Student AfroJack = new Student(4.0, Computer Science)

This just creates an object with them variables set ( if your constructor has a set function )



Sorry I cant post more java code I'm On My Phone

it's fine, thanks for the tip :)

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Crueld Hand said:

so I've just recently started learning about objects which I'm sure is obvious if you look at the threads I've been posting. What I don't really get is what the point of constructors even is. Aren't they essentially just variables?
A constructor is definitely not a variable. When you invoke a constructor, you are essentially creating an instance of a class.
This instance is what most people like to call an object.

So for example:

Account account1 = new Account();

Here you have just created an instance of Account, or you have just created an object of the Account class.
Now, objects contain variables and methods.

To interact with these variables, an object usually invokes the appropriate method to change/set these variables.
Example:

Account account1 = new Account();

boolean deposited = account1.withdrawal(35.99);

System.out.println("Withdrawal successful: " + deposited);


// Located in another class... :

class Account {

    double balance;

    public boolean withdrawal(double amt) {

        if( amt > balance ) // user wants to withdrawal more than they have!

            return false; // not a good transaction

        else // user has enough money to make this withdrawal

            amt -= balance; //deduct the withdrawal amount from your current account balance

            return true;  //return the idea that this transaction was successful

    }


Using the second skeleton of code you provided draws you away from the idea of encapsulation.
The code you provided:

public class Account {

	double balance;

	double initialBalance;

	

	initialBalance = balance;

	balance = 0.0;

	

	

}

This code works fine. But it can get ugly when using it. Let's build off of this.
If I wanted to make a withdrawal, I'd have to repeatedly program the same algorithm for each withdrawal.
Example:


//somewhere in public static void main(...) {...

Account account1 = new Account();


double balance = account1.balance;

double withdrawal = 35.99;

boolean successful;

if (balance < withdrawal ) 

    succesful = false;

else {

    successful = true;

    account1.balance = account1.balance - withdrawal;

}

System.out.println("Successful transaction: " + succesful);

Now you'll have to repeat most of this code for each withdrawal you make. Compare that to:

Account account1 = new Account();

boolean deposited = account1.withdrawal(35.99);

System.out.println("Withdrawal successful: " + deposited);


As you can see, the object makes a call to withdrawal with a paramater of 35.99
Behind the scenes we don't know what's happening(as an end-user) but we know more or less what is happening. (A withdrawal)

So in short, when you call a constructor, the JVM is allocating a spot in memory for your object. This spot in memory will hold the variables/methods of this object.
Primitive variables, have smaller space in memory because you cannot invoke any methods on them and well... variables don't have variables.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users