Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 27th, 2007, 06:49 AM
Registered User
 
Join Date: Jul 2007
Location: vijayawada, andhra pradesh, India.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need some help on basics

Hi,
    In an java application if we specify the class that is having a main() method as public must be saved with the name same as class name. If we won't specify the class as public and saved with another name it is compiled. what's the reason behind it.
Eg:
//Test.java
public class Test.java{ //here the program compiled
public static void main(String[] args){

//Test.java
public class NewName{ //here program won't compile
public static void main(String[] args){

//Test.java
class NewName{ //suprisingly program compiled!
public static void main(String[] args){

Reply With Quote
  #2 (permalink)  
Old August 22nd, 2007, 11:33 AM
Authorized User
 
Join Date: May 2007
Location: , , .
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

no problem for compilation,
it will compile.. and will create NewName.class
BUT if u trying run ( Test ) it will produce run time exception...




http://studyjava.org/forums/
Reply With Quote
  #3 (permalink)  
Old September 6th, 2007, 12:51 PM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You are allowed to define more than one class in a file, but only one public class,which must be named as the file. So you could if you wanted do:

Code:
//Test.java
public class Test {  // Named as the file (nb .java is not required)
    public static void main(String[] args){ System.out.println("Test"); }
}

class NewName{     //because we're allowed to define other classes in this file
    public static void main(String[] args){ System.out.println("NewName"); /*Won't run*/}
}
Cheers,
Charlie

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #4 (permalink)  
Old September 9th, 2007, 03:44 AM
Registered User
 
Join Date: Sep 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the generated class will be NewName.class and
$>java NewName

will also produce the desired output.

However

$>java Test

will fail at runtime.

Rgds
Jo


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
asp basics help - namespaces ( I think?) rmullins1 ASP.NET 2.0 Basics 4 September 3rd, 2008 05:42 PM
problems with basics help becky Visual Web Developer 2005 0 September 13th, 2006 05:49 PM
Basics Andy122 Dreamweaver (all versions) 3 January 2nd, 2005 03:36 PM
PHP basics limsra BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 August 18th, 2004 08:28 AM



All times are GMT -4. The time now is 03:24 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.