LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   basic java programming question (https://www.linuxquestions.org/questions/programming-9/basic-java-programming-question-365711/)

nkoplm 09-21-2005 04:31 PM

basic java programming question
 
help!

please concider the followwing code:
Code:


Customer c;  //c will refer to a customer
c = new Customer();  //c refers to a newly created customer

simmilarly:
Code:


        int[] myarray;
        myarray = new int[10];

the first excerpt is from my csc textbook. the second performs a simmmilar operation.

i sort of get that these both "create" a new object(?, pointer?, instance?) of whatever you are trying to initialize(?). but (as you might have guessed) im still a little fuzzy on understanding it all completely.

could anyone tell me

A)what exaclty is this doing

and B) what is the difference between what the first line does and the second line does.


thanks for the help.

Jestrik 09-21-2005 04:45 PM

The first line of the code declares the object in memory. The second line instantiates the object.

Think of it like a person -

The first line declares the name of the person.
The second line gives meaning to the person, i.e. brings it to life and gives it properties and sets what type of person this is.

Hmm, I'm not very good at explaining this, try and look for some tutorials on Google, i'm sure they're better than me!

chrism01 09-21-2005 11:46 PM

Jestrik is right. In other (loose) terms, think of line 1 as defining/declaring a data structure ( eg C struct ) and line 2 as producing an 'instance' of that structure (with data) that you can actually use.
Chris
PS Hi to Jestrik from another Hampshire-ite

spooon 09-22-2005 01:37 AM

In Java, object values are effectively what are pointers in other languages. When you declare "Customer c;", it is initially set to NULL. Then when you do "new Customer()", it allocates memory and creates the object; then "c = ..." makes "c" point to this new object.


All times are GMT -5. The time now is 12:47 AM.