Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 27th, 2007, 05: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){

 
Old August 22nd, 2007, 10: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/
 
Old September 6th, 2007, 11:51 AM
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
 
Old September 9th, 2007, 02: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






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 04:42 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 07:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.