Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

array.length

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] i = {1,2,3};
System.out.println(i.length);

Why this doesn't compile?
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should compile perfectly fine. Please retry.
 
Galina Zapletnuka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried. The compiler says <identifier> expected.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that the problem is with that line?
 
Galina Zapletnuka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ArrayFun
{
int[] i = {1,2,3};
System.out.println(i.length);
}

The compiler says:
galina@dell-desktop:~/java training/SCJP$ javac ArrayFun.java
ArrayFun.java:4: <identifier> expected
System.out.println(i.length);
^
ArrayFun.java:4: <identifier> expected
System.out.println(i.length);
^
2 errors
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is incorrect. Your SOP should be inside a method or constructor

 
Galina Zapletnuka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Thank you very much.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're preparing for the test, you should also make a point of knowing why it wasn't working in its original form.
 
Galina Zapletnuka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know if I'm following what you mean, but I originally started playing around with K&B's exercise

class Zippy
{
String[] x;
int[] a[] = {{1,2}, {1}};
Object c = new long[4];
Object[] d = x;
}

and then added some array declarations to see if they work.
Now that you've pointed out the SOP problem, I've put the above code in main and it doesn't compile, because x is unassigned.
Why does it compile without main?
Am I missing out on something? I'm a novice programmer and shy about asking stupid questions, but still?
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Galina,

There are a few rules that Java programming follows. The code works fine when you put it outside main because in that case all members are class level variables and they can be initilized in any member functions. But, when you put the same inside a main function then the compiler knows that they are local variables and have to be initilized before they are used as local variables couldn't be initialized elsewhere. So the compiler shouts and asks you to initilize the variable.

Originally posted by Ben Souther:
If you're preparing for the test, you should also make a point of knowing why it wasn't working in its original form.



By this Ben wanted to know if you were preparing for the SCJP exam so he told you to get the basics right.

Why does it compile without main?



A copilable unit is one which is syntactically correct. A main method is the entry point for execution. Even if there is no main method(which means no entry point to run the code) still if the syntax of code is correct it will compile.

I'm a novice programmer and shy about asking stupid questions, but still?


We are a friendly forum and don't feel shy for asking small questions. In fact we have a forum dedicated for beginners. You can visit beginner's forum by following the link. Probably you can post your small doubts in this forum as it is better suited.

We will be happy to assist you. Good Luck..!!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Galina Zapletnuka:
int[] i = {1,2,3};
System.out.println(i.length);

Why this doesn't compile?



it does. Give us some context.... where's the rest of your code?
 
Galina Zapletnuka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Anubhav!
Thank you for taking the time to write such a detailed reply. Very helpful.
I will certainly try and make the most of your beginners forum. Thanks.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] i = {1,2,3};
System.out.println(i.length);

Why this doesn't compile?
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jagadeesh Babu:
int[] i = {1,2,3};
System.out.println(i.length);

Why this doesn't compile?



Jagadeesh Babu,
Welcome to JavaRanch.

Your question has been already answered above. Let us know if you have any other outstanding doubts.
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic