Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: I have a problem when creating a class

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have a problem when creating a class

    I do not understant why i get that error http://imgur.com/HhdVmhu

    I am following internet tutorials, and this is where i got stuck
    Attached Images Attached Images


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: I have a problem when creating a class

    What is the error? Please copy and paste the full stack trace instead of posting a screenshot.

    What code are you running? Please copy and paste an MCVE instead of posting a screenshot.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem when creating a class

    I do not know what a full stack trace is... but i will pot my code..

    public class Program {

    public static void main(String[] args) {
    Persoana acces = new Persoana ();

    }

    }
    this is in the default package


    this is the class i created

    package Program;

    /**
    *
    * @author Administrator
    */
    public class Persoana {
    String name;
    String surname;
    int age;

    }


    i keep getting that error there. its like it is not finding that class file i created...

    PS : started learning java 1 day ago...

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: I have a problem when creating a class

    What specific error are you getting?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem when creating a class

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol: class Persoana
    location: class Program
    at Program.main(Program.java:14)
    Java Result: 1

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: I have a problem when creating a class

    Are these classes in two separate files?

    Is one of them in a package?

    Have you imported the class you're trying to use?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem when creating a class

    Quote Originally Posted by KevinWorkman View Post
    Are these classes in two separate files?

    Is one of them in a package?

    Have you imported the class you're trying to use?
    Yes there are 2 classes in 2 separated files.
    Main is in <default package> ...
    I didn't import anything. i just created a new file as Java Class.

    Ps: try looking at the imgur print... that's the setup.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: I have a problem when creating a class

    I can't access imgur, which is why we ask you to copy/paste the text instead.

    If the class you're using is in a different package than the class you're in, you either need to import it or use the fully qualified name (include the package info before the class).

    Recommended reading: Using Package Members (The Java™ Tutorials > Learning the Java Language > Packages)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem when creating a class

    I just tired something... when i clicked File>new file> java class> next There is a Package tab. I didn't type anything in it, and it seems its working... In the tutorial i was following they were typing the name of the main file... I am confused

    And an off topic question, its something that troubled me about java since i started looking into it , and now that i started learning classes troubles me more...
    When i create a new project it comes with this line pre-configured

    public class NameOfProject {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    }
    }
    why is that ( public class NameOfProject { ) there?, does it have to be there?
    I understand that public static void main has to be there because the program looks for the main and starts executing instructions from there.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: I have a problem when creating a class

    Quote Originally Posted by jeffpascal View Post
    I just tired something... when i clicked File>new file> java class> next There is a Package tab. I didn't type anything in it, and it seems its working... In the tutorial i was following they were typing the name of the main file... I am confused

    I just tired something... when i clicked File>new file> java class> next There is a Package tab. I didn't type anything
    I highly recommend you ditch the IDE and use the command line and a basic text editor until you understand the basics.

    Your original problem was that you were trying to use the Persoana class inside your Program class. Since Persoana is inside a package, you have to import it to be able to use it.

    Since you modified your code so Persoana is no longer inside a package, you no longer had to import it, and your code magically worked again.

    It's not always a good idea to use the default package (no package). It's up to you though.

    Quote Originally Posted by jeffpascal View Post
    And an off topic question, its something that troubled me about java since i started looking into it , and now that i started learning classes troubles me more...

    When i create a new project it comes with this line pre-configured

    why is that ( public class NameOfProject { ) there?, does it have to be there?
    I understand that public static void main has to be there because the program looks for the main and starts executing instructions from there.
    What happened when you tried to remove the part you're confused about?

    You can't have a method outside of a class. So to have a main() method, you have to have a class. I highly recommend using a basic text editor and the command line until you understand basics like that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem when creating a class

    Quote Originally Posted by KevinWorkman View Post
    I highly recommend you ditch the IDE and use the command line and a basic text editor until you understand the basics.

    Your original problem was that you were trying to use the Persoana class inside your Program class. Since Persoana is inside a package, you have to import it to be able to use it.

    Since you modified your code so Persoana is no longer inside a package, you no longer had to import it, and your code magically worked again.

    It's not always a good idea to use the default package (no package). It's up to you though.



    What happened when you tried to remove the part you're confused about?

    You can't have a method outside of a class. So to have a main() method, you have to have a class. I highly recommend using a basic text editor and the command line until you understand basics like that.
    I understand. I use the IDE because the tutorial i am following uses it, and i understand better. If you know other tutorials that use text editor, i will use them. Thank you for the answers.
    ps: i use this http://www.functionx.com/java/
    Last edited by jeffpascal; June 23rd, 2014 at 02:28 PM.

Similar Threads

  1. [SOLVED] creating an iterator without creating a class for it
    By Lenjaku in forum Collections and Generics
    Replies: 6
    Last Post: January 22nd, 2014, 02:17 AM
  2. Problem creating text file using the using class.getResource()
    By Pin Head in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 14th, 2013, 01:27 AM
  3. Replies: 1
    Last Post: November 19th, 2012, 04:44 AM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. Help with creating a class
    By cdawg_2010 in forum Loops & Control Statements
    Replies: 4
    Last Post: November 1st, 2010, 07:04 AM