Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

whaz wrong withthis?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class InitClass
{
public static void main(String args[])
{
InitClass obj = new InitClass(5);
}
int m;
static int i1 = 5;
static int i2 ;
int j = 100;
int x;
public InitClass(int m)
{
System.out.println(i1 + " " + i2 + " " + x + " " +m);
}
{j = 30; i2 = 40 //Instance Initializer
static { i1++;} // Static Initializer
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Raji Addepalli:
public class InitClass
{
public static void main(String args[])
{
InitClass obj = new InitClass(5);
}
int m;
static int i1 = 5;
static int i2 ;
int j = 100;
int x;
public InitClass(int m)
{
System.out.println(i1 + " " + i2 + " " + x + " " +m);
}
{j = 30; i2 = 40 //Instance Initializer
static { i1++;} // Static Initializer


Raji,
A couple things. First of all, please place your code within "CODE" tags. You can now use the UBB code buttons at the bottom to do this and it makes reading someone's code much easier as it preserves indentation.
Also, if you have a problem with your code, please don't ask "What's wrong with this?" If you're getting a specific error, please tell us what the error is and we can figure it out a whole lot faster. Even telling us if you're experiencing a copiler error vs. a run-time error can be a big help.
In this case, however, it looks like you're missing two '}'. One at the end of your instance initializer and one at the end of the class definition. Also, you're missing a ';' at the end of the second statement of the instance initializer.
Corey
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Raji Addepalli:
public class InitClass
{
public static void main(String args[])
{
InitClass obj = new InitClass(5);
}
int m;
static int i1 = 5;
static int i2 ;
int j = 100;
int x;
public InitClass(int m)
{
System.out.println(i1 + " " + i2 + " " + x + " " +m);
}
{j = 30; i2 = 40 //Instance Initializer
static { i1++;} // Static Initializer


im sure this code will not compile , the compiler will complain illegal start of method
[ January 14, 2002: Message edited by: vivek sivakumar ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add the ";" and "}" at the 16 line(at the end of line 'j = 30; i2 = 40'),and "}" at the end of code, It will compile and run without any error.
It give the result:
6 40 0 5
hope it can help!
[ January 14, 2002: Message edited by: Seany Iris ]
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic