Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-21-2005, 04:31 PM   #1
Registered: May 2005
Distribution: Fedora
Posts: 92

Rep: Reputation: 15
basic java programming question


please concider the followwing code:
Customer c;  //c will refer to a customer
c = new Customer();  //c refers to a newly created customer
	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.
Old 09-21-2005, 04:45 PM   #2
Registered: Dec 2003
Location: Hampshire, UK
Distribution: gentoo, Debian Sarge, Slack 9.1
Posts: 206

Rep: Reputation: 30
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!

Last edited by Jestrik; 09-21-2005 at 04:51 PM.
Old 09-21-2005, 11:46 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,378

Rep: Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755
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.
PS Hi to Jestrik from another Hampshire-ite
Old 09-22-2005, 01:37 AM   #4
Senior Member
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
basic linux programming/compiling question (permissions error) Godsmacker777 Programming 11 03-17-2005 11:35 AM
java programming question! redhatrosh Programming 2 03-15-2005 12:51 PM
Basic Linux Programming Question (QT terminal control) Boffy Programming 5 09-09-2004 12:44 PM
A very basic newbie question on Java lmellen Programming 5 07-19-2004 03:59 PM
Basic Java arithmetic question chr15t0 Programming 2 07-05-2003 01:32 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration