• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Initializers

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am executing the following program.

public class MoreInitializers
{
int noOfdays = 7 * NO_OF_WEEKS;//1
System.out.println("noOfdays : " + this. noOfdays);//2
static int NO_OF_WEEKS = 52;//3
public static void main(String args[])//4
{
MoreInitializers obj1 = new MoreInitializers();
System.out.println("noOfdays : " + obj1.noOfdays);
System.out.println("NO_OF_WEEKS : " + NO_OF_WEEKS);
}
}

Now at line 2 - I am trying to print the value of "noOfdays" . Basically
I tried giving the variable noOfdays and this.noOfdays in the println statement.
I get this error in either case at the compile time:
D:\Java_prgms\gc\MoreInitializers.java:4: <identifier> expected
System.out.println("noOfdays : " + this. noOfdays);//2
^
1 error

Tool completed with exit code 1

Please explain why this error occurs?
Is it valid to access and print the value of variable noOfdays at line 2? If yes then how cann I access it?

Thanks,
Rekha
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The print statement should be inside a method ... Hence the error
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You forgot to put the statement inside the braces so that it would be recognised as an instance initializer

Please have a look ..


 
rekha devan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!!
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rekha,

I do not think a 'System.out.println' is permitted in the place where you have put it. A class definition can have only instance variables and methods.

If you want to print the value at the point you have mentioned, one of the ways of doing it would be as

public class TClass
{
int noOfdays = 7 * NO_OF_WEEKS; //1
public void sop()
{
System.out.println("noOfdays : " + this. noOfdays); //2 - Enclose this in a method
}
static int NO_OF_WEEKS = 52; //3
public static void main(String args[]) //4
{
TClass obj1 = new TClass();
System.out.println("noOfdays : " + obj1.noOfdays);
System.out.println("NO_OF_WEEKS : " + NO_OF_WEEKS);
obj1.sop(); //call the method once the object is created

}
}

Hope this helps!
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tapan,

Class declaration can contain instance initializers too...

So Kumar's code is valid.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Seb Mathe:
Hi Tapan,

Class declaration can contain instance initializers too...

So Kumar's code is valid.



And Static initilizers too.
 
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everyone for explaining this. I have seen this before..where you have just a System.out.println command outside of a method.

But the one thing I don't understand is how we can get away with having a SPACE between the "this." and the "noOfdays" ???

I see that it compiles and runs, but I don't know why...I thought that the statement would have to have the "this. " and "noOfdays" together like this:
System.out.println("noOfdays : " + this.noOfdays);

Is this really how the argument of the println method works with any Object.identifier (or should I say Object. identifier) ?

Thanks for any explanation.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Java ignores all unnecessary spaces and newline chars whereever they are not needed. That means you are free to write code full of spaces and new lines:


the above code is valid

similarly

Compiles fine

Hope this helps
[ October 10, 2005: Message edited by: Sandeep Chhabra ]
 
This tiny ad is wafer thin:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic