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

Initialization

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one of you please explain the below code.

public class initialize
{
public initialize(int m)
{
System.out.println(i+","+j+","+k+","+x+","+m);
}
public static void main(String[]args)
{
initialize obj=new initialize(x);
}
static int i=5;
static int x;
int j=7;
int k;
{ //1if we remove these braces then why it is giving error
j=70;x=20;
}//2
static
{
i=50;
}
}

if we remove the lines //1 and //2 why it is giving error


Thank you
Sampath
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi



if we remove the lines //1 and //2 why it is giving error



Inside a class and outside of any block within a class only possibles are
1) Declaration only
2) Declaration along with intialization in same line


but in your code declaration and assignmant are in different line

if u add block.......it act as a non-static block
[ September 30, 2005: Message edited by: vidya sagar ]
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class body contains :

- declaration fo constructor(s)
- declarations of memebers(fields, methodes, classes, interfaces)
- static initializers
- instance initializers

Regarding java specs, instance initializers must be a block.
[ September 30, 2005: Message edited by: Seb Mathe ]
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for both of you guys for the explanation.


Sampath
reply
    Bookmark Topic Watch Topic
  • New Topic