• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Need help on complier error

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get a type expected error for this code. Could anyone enlighten me as to why?
public class Temp
{
int number = 0;

if(number < 10); //complier reports Type expected

public static void main(String [] args)
{
Temp app = new Temp();
}
}
I am trying to get some logic code to work in the class instead of in main. Most of the textbook examples have the code in main and I was trying to do something different. Is this a wise course?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think u should put the code (if condition) in a method and try to call that method .I think that will solve ur problem.
Bye.
sudha

[This message has been edited by Sudha Rajaram (edited August 31, 2000).]
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
yes sudha ,u r right.
regards
deekasha
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you put it in an instance block it will work

------------------
Regds.
Rahul P. Mahindrakar
 
B Young
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info. I am not planning on riding down this box canyon anymore.
However, I am the curious type and if anyone has an additional insight on why the initialization block works, please write back. My limited understanding of an initialization block is it is executed prior to the creation of the object of the class. So how does creating an "if statement" calm the compiler? In my mind this should make things worse, which is not the case. Executing the "if statement" prior to initializing the variable "number" should cause problems. I guess that I must have faulty understanding/reasoning on how the compiler actually works here.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the following in the Java Language Specification:
A class body may contain declarations of members of the class, that is, fields (�8.3) and methods (�8.4). A class body may
also contain static initializers (�8.5) and declarations of constructors (�8.6) for the class.
ClassBodyDeclaration:
ClassMemberDeclaration
StaticInitializer
ConstructorDeclaration
ClassMemberDeclaration:
FieldDeclaration
MethodDeclaration
In other words, the way I read it is that the only things the compiler will read directly in a class are Field Declaration (variable declaration)/static variable declaration & initialization and Method (including constructor) declaration.
Therefore any statements (like your "if statement" need to be in one of these (variable declaration or method declaration).
Marilyn
 
B Young
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn, I think you hit the nail on the head with this fine detective work. I thank you for this informative reply. I did not even think to look in the Java Language Specs for this info. Too busy reading the textbooks.
 
pie. tiny ad:
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