Results 1 to 8 of 8
  1. #1
    srikanth is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Default class and file name

    hello guys and gals.
    hey i want to know whether i can name the file and the class differently?

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Name of class and file name must be same. Files are sometimes called compilation units in computer science, just use some throwaway names until you get acclimatized to making file name and class name the same.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    No you can't do it. It must be same.

    By the way, please read our FAQ page before post the next question. You have posted your question in wrong place. At this time I'll move it to correct place.

  4. #4
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default Watermark in java class file

    Hi

    I would like to know how to embed watermark in Java class file and decode that watermark class file to get back the watermark value.

    I will be appreciate if any one can provide the source code.

    I'hv tried many time to get the bytecode of the class file to embed the watermark value but failed. Should i used the class reader to read the class file but where i must to called the class file and how.

    I include some code that i don't know where to get my .class inside this code:

    package test;

    import javax.swing.JOptionPane;

    public class ClassLocationTest {
    public static void main(String[] args) {
    JOptionPane.showMessageDialog(null, getClassLocation(String.class));
    }

    public static String getClassLocation(final Class<?> pClass) {
    final String location, name;
    name = pClass.getName().replaceAll("\\.", "/") + ".class";
    location = ClassLoader.getSystemResource(name).getPath();
    return location;
    }
    }



    I hope any one can help me immediately because i must to submit it next week.



    Thanks you
    Azura

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default Regarding class name and file name

    If u declare the class as public then class name and file name should be same or else u can give differnt name.

  6. #6
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    class name and file name should be same or else u can give differnt name.
    Could u elaborate this i don't understand

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    I have given this reply for class and file name and not for what you asked.
    Sorry for the confusion

  8. #8
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    All public classes must be in a file with the same name, unless its contained within another class declaration (read in code below).

    However there are default, protected and private classes where they don't need to be in a file with the same name.

    It comes down to visibility how these different classes are accessed:
    default: package level
    protected: subclass level, needs to be contained within a public class declaration
    private: class level, needs to be contained within a public class declaration

    Consider the following:
    Java Code:
    // PublicClass.java
    
    package DefaultPackage;
    import DefaultPackage.AnotherClass.ProtectedClass;
    import NewPackage.OMGAnotherClass.ZOMGWillItStop;
    public class PublicClass {
    	DefaultClass dc = new DefaultClass(); /* can see it */
    	ProtectedClass pc; /* can see it */
            ZOMGWillItStop s; /* yep... thats legal */
    }// PublicClass.java
    
    
    //PublicClass.java
    class DefaultClass {
    }// PublicClass.java
    
    // AnotherClass.java
    package DefaultPackage;
    
    public class AnotherClass {
    	DefaultClass dc = new DefaultClass(); /* can see it */
    	protected class ProtectedClass {
    	}
    }// AnotherClass.java
    
    // YetAnotherClass.java
    package NewPackage;
    
    import DefaultPackage.*;
    public class YetAnotherClass extends AnotherClass {
    	// DefaultClass dc; /* can't see it */
    	ProtectedClass pc; /* we can see it because we are a subclass of AnotherClass */
    	private class PrivateClass {
    	}
    }// YetAnotherClass.java
    
    // OMGAnotherClass.java
    package NewPackage;
    import DefaultPackage.*;
    public class OMGAnotherClass extends PublicClass {
    	// PrivateClass p; /* can't see it */
    	// DefaultClass dc; /* can't see it even though we are a subclass of PublicClass
    // OMGAnotherClass.java
    package NewPackage;
    import DefaultPackage.*;
    public class OMGAnotherClass extends PublicClass {
    	// PrivateClass p; /* can't see it */
    	// DefaultClass dc; /* can't see it even though we are a subclass of PublicClass
    	/* valid */
    	public class ZOMGWillItStop {
    	}
    }// OMGAnotherClass.java
    When it all compiles they are put into there own little .class files.

    Example, the default will be in default.class (because its not contained within a class declaration) but the ProtectedClass will have a filename of AnotherClass$ProtectedClass.class symbolising the package AnotherClass, class name ProtectedClass
    Last edited by hawaiian robots; 04-16-2009 at 02:26 PM.

Similar Threads

  1. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 09:17 AM
  2. Generate a Class file
    By Daniel in forum New To Java
    Replies: 6
    Last Post: 04-20-2009, 05:37 AM
  3. Replies: 0
    Last Post: 05-13-2008, 06:16 AM
  4. Convert my class file into a exe file
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •