Categories

Main
Database
Java
Microsoft.NET
Adabas
DB2
Informix
Microsoft SQL Server
MySQL
Oracle
Pervasive.SQL
PostgreSQL
Sybase
Other
ASP
ColdFusion
Crystal Reports
Delphi, C etc
JAVA
Microsoft.NET
Perl and the DBI
PHP
ANSI SQL
Unix Shell Scripts
Visual Basic
XML & XSLT
Corel Paradox
FileMaker
Microsoft Access
Microsoft Excel
Other PC Databases
Applications & Tools
Database Concepts & Design
EJB programming & troubleshooting
EJB design
General J2EE
XML & Web services
Web tier: servlets, JSP, Web frameworks
Performance and scalability
Industry news
TSS feedback
Mobicents Contributors
Mobicents Users
JSLEE Resource Adaptor Types
Planning JavaOne 2008
Sun Tech Days
Other Java conferences
Binary Web Services and XML
Metro and JAXB
GlassFish
GlassFish Plugins
Project jMaki
GlassFish WebTier
Mural
Java Development Tools
Java WS & XML Community News
JAXP
Java SE
6uN Early Access
Java Quick Starter
Java SE Snapshots: Project Feedback
JCK Forum
Feedback and Suggestions
JDK Distros
JDK Open Source
General JSR Discussion
JCP 2.6
JXTA Community Forum
ME Interest
ME Cool Apps
ME General Help
ME Feedback & Suggestions
ME Application Developer Interest
Blu-ray Disc Java
ME Developer Days
Squawk
Mobile Developer Alliance
OpenCable
LWUIT
JavaFX Script Language Discussion
OpenJFX General Discussion
Scene Graph
General Performance Discussion
Your Java Career
NetBeans 6.0
Servlets
JSP
JSF
Portals and Portlets
EJB and Other Java EE Technologies
Distributed Java
Object Relational Mapping
JDBC
Web Services
Swing / AWT / SWT / JFace
JNLP and Web Start
Java Micro Edition
Sockets and Internet Protocols
Threads and Synchronization
Performance
Applets
I/O and Streams
Other Java APIs
Game Development
Java in General (beginner)
Java in General (intermediate)
Java in General (advanced)
Programmer Certification (SCJP)
Developer Certification (SCJD)
Associate Certification (SCJA)
Web Component Certification (SCWCD)
EJB Certification (SCBCD)
Mobile Application Certification (SCMAD)
Architect Certification (SCEA)
Web Services Certification (SCDJWS)
XML Certification
Product and Other Certifications
Mock Exam Errata
Sun Certification Results
Authors' Corral
Book Reviews
Events
Bunkhouse Porch
Teachers' Lounge
Testing
OO, Patterns, UML and Refactoring
IDEs, Version Control and other tools
Ant, Maven and Other Build Tools
Linux / UNIX
Mac OS
HTML and JavaScript
XML and Related Technologies
Agile and Other Processes
General Computing
Security
Groovy
Scala
Other Languages
Struts
Application Frameworks
Other Open Source Projects
BEA/Weblogic
IBM/Websphere
Oracle/OAS
Apache/Tomcat
JBoss
Other Java Products and Servers
JavaRanch
Cattle Drive (java college)
Moderators Only
Trash Can
Jobs Offered
Jobs Wanted
Jobs Discussion
Meaningless Drivel
Programming Diversions
Blatant Advertising
Java Announcements
New To Java
Advanced Java
Java Applets
Networking
Threads and Synchronization
Java 2D
AWT / Swing
SWT / JFace
CLDC and MIDP
CDC and Personal Profile
Sun Java Wireless Toolkit
Enterprise JavaBeans
JavaServer Pages (JSP) and JSTL
Java Servlet
JavaServer Faces
Web Frameworks
Database
XML
Lucene
NetBeans
Eclipse
IntelliJ IDEA
JCreator
Other IDEs
Java Tutorials
Java Tips
Jobs Discussion
Jobs Offered
Jobs Wanted
Professional Certification
Forum Lobby
Java Blogs
Introductions
Reviews / Advertising
Suggestions & Feedback

Resources

Java Database
Linux
Coding
Mobile
Hardware
Software Development
Software Development
iOS,OS X
iOS,OS X
ORACLE
IBM DEVELOPER
IBM DEVELOPER
MSDN
MSDN


Tags

Java in General (beginner)

Java in General (beginner) No question too simple or small . . .

Very basic but tough question


We know we can declare a variable in a class scope. Like below:code:class MyClass{int a;String b;public static void main(String args[]){System.out.println("Value of a is:"+a);}}But we can't call any method in a class scope. Like below:code:class MyClass{int a;String b;System.out.println("Value of a is:"+a);}It will give some error. Upto that it is OK. But my question is why java is not allowing us call a method in class scope? Very basic question but I am confused about that. Can anyone please help me to answer this.--------------------Abhinaba basuSoftware Engineer,Kaizen Informatics Pvt LtdMy site | My Blog | My office

   
   
   
   
   
      Moved to Java in General (beginner).--------------------[Smart Questions] [JSP FAQ] [Books by Bear]  [Bear's FrontMan] [About Bear]

   
   
   
 

      Its ok. but can u answer this? It is basic question, but anyone has any idea about that? Why java is not allowing. Dont answer like, main only can call a method, so a method call should be inside main, or inside another method like this. I need the proper answerwhy java is not allowing us?We can call constructor also. Is not it? So what is java's problem on that(method calling)?--------------------Abhinaba basuSoftware Engineer,Kaizen Informatics Pvt LtdMy site | My Blog | My office

   
   

 
   
      So suppose it was possible to have method calls directly inside a class body - could you tell us what you think that should mean? When should that code be executed?--------------------Java Beginners FAQ - JavaRanch SCJP FAQThe Java Tutorial - Java SE 6.0 API documentation

   
   

 

      Sure you can do that, just get the syntax right:code:public class MyClass{{System.out.println("Hello, world!");}}

   
   

 
   
      Hint that explains why Jeff's code works:instance initializer--------------------Java Beginners FAQ - JavaRanch SCJP FAQThe Java Tutorial - Java SE 6.0 API documentation

   
   
   
 

      Yes it is static initializer. But dr we can call methods like,code:class A{static int x;String g = new String("abhinaba");String h = g.replace("a","b");public static void main(String[] args) {//Some code here}}but the return type of println is void. So we can't initialize anything using this.But method call is possible, which returns a vlue.--------------------Abhinaba basuSoftware Engineer,Kaizen Informatics Pvt LtdMy site | My Blog | My office

   
   

 

      quote:Originally posted by Abhinaba Basu:but the return type of println is void. So we can't initialize anything using this.Yo and behold, you can:code:public class MyClass {static String message;public static void main(String[] args) {System.out.println(message);}static {System.out.println(message="Hello, world!");}}

   
   

   
   
      you cant because main is where  your code starts excuting. if u put println directly inside a class without a main there is no execution taking place. call a function called go(); inside main and then put your println inside go();likepublic class example{int a;public static void main(String[] args) {go();}go(){System.out.println(a);}}its the main that causes the ball to roll, no main nothing happens.regardsvinay.

   
   

 

      quote:Originally posted by Vinay Thippeswamy:no main nothing happens.FALSE!Classloading happens. This code prints "Hello, World" and I don't see no main!code:public class MyClass {static {System.out.println("Hello, world!");}}--------------------There is no emoticon for what I am feeling!

   
   

   
   
      HelloWorld prints but you get an exception if you do that.C:\pj>java MyClassHello, world!Exception in thread "main" java.lang.NoSuchMethodError: mainthats not good OO either[ June 07, 2006: Message edited by: Vinay Thippeswamy ]

   
   

   
   
      Then try thiscode:public class MyClass{static{System.out.println("Hello, world!");System.exit(0);}}--------------------M.VIDYASAGARSCJP 1.4,SCWCD 1.4,SCBCD 1.3,OCA 9i Learning Spring

   
   

 

      quote:Originally posted by vidya sagar:Then try thiscode:public class MyClass{static{System.out.println("Hello, world!");System.exit(0);}}Excellent!

   
   

 
   
      quote:Originally posted by Abhinaba Basu:Yes it is static initializer. But dr we can call methods like, ...No, that is not astatic initializerin Jeff's example. It is aninstance initializer- that's not the same as a static initializer.[ June 08, 2006: Message edited by: Jesper Young ]--------------------Java Beginners FAQ - JavaRanch SCJP FAQThe Java Tutorial - Java SE 6.0 API documentation

   
   
   
 
   
      Back to the original question: "Why can't you put executable code outside of a method (or initialiazation block) but still inside a class?"Short answer:  Because the people that designed the Java language said so.Long answer: I'm not sure exactly, but it probably has to do with the fact that a lot of Java syntax comes from C++.  However, I think the best way to explore this option is the questions that Jesper asked above:  What would it mean if you COULD do this?Layne[ June 08, 2006: Message edited by: Layne Lund ]--------------------Java API DocumentationThe Java Tutorial


Related Links

Load package or class?
CLASSPATH in .bat file
Printing the ArrayList
outOfMemoryError
INitialization Err
What is lazy initialization.
C# to Java
how to test for which primitive type a varible is
Creating several instances of a program
Need Help with ArrayList - NullPointerException
basic doubt!!
Passing Parameters
What is the root class for all java classes?
Beginning With Databases
What is Remote Class?
scan a new file in a directory