LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-15-2005, 11:45 AM   #1
redhatrosh
Member
 
Registered: May 2004
Location: Student of University of Mumbai, Maharastra State, India
Distribution: Redhat Linux 9.0, Knoppix LIVE CD, Ubuntu Live CD, Kubuntu Live CD
Posts: 483

Rep: Reputation: 30
Array declaration in class or main function???


One of my code in a java program

class marks{
int[] array;
array = new int [5];
}


... on compilation gives me an error and expects an identifier in the statement ........

array = new int [5];

pointing at the equal to sign ('=') ........

when declared in.......

public static void main(String args []){
int[] ar;
ar = new int [5];
}

gives no error...can someone tell me about this?
 
Old 03-15-2005, 12:15 PM   #2
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
I am no expert in JAVA but I guess you cannot have initializing statements in class declaration.
maybe you wanted that inside the constructor.
 
Old 03-15-2005, 12:19 PM   #3
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
zeropash is on the right track. Only *CONSTANT* initializers are permitted in the class declaration. so int x=5; is fine. int x=5*class.func(); is not.

If you want to allocate an array, put it in your constructor.
 
Old 03-15-2005, 12:32 PM   #4
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
class marks{
int[] array = new int [5];
}

I guess should be fine too.
 
Old 03-15-2005, 02:13 PM   #5
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
zeropash: It should *NOT* be ok. "new" is an operator that allocates memory at runtime, and is thus NOT a constant initializer.
 
  


Reply


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
Difference between including a header file and 'class' declaration in C++ scoTtkIm Programming 1 09-09-2004 04:23 AM
Arguments in PHP function declaration Parksy Programming 5 07-04-2004 04:22 PM
Is there a default size limit to a dictionary, list or array declaration ? Linh Programming 2 06-08-2004 11:35 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Problem with function declaration Linh Programming 3 04-26-2004 04:58 PM


All times are GMT -5. The time now is 11:54 PM.

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