• Post Reply Bookmark Topic Watch Topic
  • New Topic

I want to create my library to use with Netbeans

 
Filipe Madureira
Ranch Hand
Posts: 42
1
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

I'm using NetBeans at the moment.

My professor showed us how we can create a class without any Main method and a list of useful stuff i can use on all my projects. I tried to do the same thing at home but i can't figure out how to make it work.

I wanted to start to code and grow my own library as long as i complete exercises for the class so the inability to achieve this is grinding my gears ahah.

Basically, i create a library project called A, add my ".java" files and when i try to use the project A inside my project B i can't manage to accomplish stuff. I don't even know how to import or call my methods from the library and google is not helping me, it's frustrating.

This will clear everything up:


FilsLibrary is my project without Main method. LproEsercizi is the project in which i want to be able to use my library methods. i right click the "libraries" folder, add the project and that's it. I can't figure out how to continue. Help please
 
Tim Moores
Saloon Keeper
Posts: 3335
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My advice is: forget about NetBeans for the moment. It is a complex piece of software that takes time to master. You're better off learning Java instead. Use a programmer's editor to write the code, and the command line to compile and run it.
 
Filipe Madureira
Ranch Hand
Posts: 42
1
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:My advice is: forget about NetBeans for the moment. It is a complex piece of software that takes time to master. You're better off learning Java instead. Use a programmer's editor to write the code, and the command line to compile and run it.


That's not an option. Use use Netbeans in class and during exams.
 
Filipe Madureira
Ranch Hand
Posts: 42
1
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*We use.

Where is the edit option in this forum...?
 
Campbell Ritchie
Marshal
Posts: 53171
124
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds to me that you want utility classes. That is a design pattern with the following features:-
  • 1: All members of the class are static.
  • 2: All public methods act as functions: you give them an input and you get a result or an effect.
  • 3: You never need any instances, so you make the constructor private. You can read about private constructors in the Java® Language Specification; that is one of the few sections which I think are easy to read.
  • 4: All its exposed fields are constants.
  • Utility classes exist only to provide services for other code. Here is an example:-Note that you have to write documentation comments for all those public members. You can add other constants or methods as required. Note that no methods from Object are overridden; you cannot call instance methods on an uninstantiable class, so it would be pointless to override toString equals hashCode clone or finalize.
    Other methods you might want in a utility class might swap two elements in an array, or sort an array. The most useful utility class might be one for reading from the keyboard with a Scanner object.
     
    Filipe Madureira
    Ranch Hand
    Posts: 42
    1
    Chrome Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Sounds to me that you want utility classes. . . .


    Yes that's exactly what i want to do! Thank you very much for clearing up a lot of doubts i had.
    How can i use utility classes on my main project though?
     
    Dave Tolls
    Ranch Hand
    Posts: 2378
    25
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    From that screenshot you have added the project as a library.
    So what issue (exactly) are you having?

    Can you show code that has issues, and post any error messages you have from the compiler (I can see one class, for example, that has an error)?

     
    Campbell Ritchie
    Marshal
    Posts: 53171
    124
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Filipe Madureira wrote:. . . How can i use utility classes on my main project though?
    You will have to expand that class to get this code to work.

    Don't quote the whole of a preceding post; that simply duplicates things without adding value. I have removed most of a quote.
     
    Filipe Madureira
    Ranch Hand
    Posts: 42
    1
    Chrome Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is what i do:

    1. Right click the Libraries folder of the project i want to add my utility classes to.
    2. Select the option "add project" and from the list i choose the "FilsLibrary" project which is the project with my utility classes containing useful methods.



    I remember our professor telling us this, and nothing else so i'm kinda blocked. Now i'd like to use the methods in "FilsLibrary" and import the file so i'm actually able to use them but i don't know how.

    This is the current folder i'm working on:


    And basically what i don't understand how to do is this (check the uppercase comments):
     
    Dave Tolls
    Ranch Hand
    Posts: 2378
    25
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When posting code please copy.paste the code and not screenshots.
    It's easier to follow that way.

    OK, at the top of your class you would import the class(es) you want to use...just as the code currently imports the Scanner class.

    In the code you would then use them.
     
    Filipe Madureira
    Ranch Hand
    Posts: 42
    1
    Chrome Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dave Tolls wrote:
    OK, at the top of your class you would import the class(es) you want to use...just as the code currently imports the Scanner class.

    In the code you would then use them.


    I posted in 'beginning java' because i'm a beginner. I don't know how to do that. I'm asking for help.

    This is my utility class:



    This is my project where i want to use the method 'sumTwoIntegers':



    I have no idea what's wrong.
     
    Dave Tolls
    Ranch Hand
    Posts: 2378
    25
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What error(s) is it giving you?
     
    Junilu Lacar
    Marshal
    Posts: 10409
    125
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
     
    Filipe Madureira
    Ranch Hand
    Posts: 42
    1
    Chrome Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Everything should work properly, i did exactly what's written in that part of the page. I seriously can't comprehend what's wrong :/

    Dave Tolls wrote:What error(s) is it giving you?


    in the



    i get a " '.' expected " error.

    Meanwhile, in the

    line i get a "Cannont find Symbol Test1" error.
     
    Junilu Lacar
    Marshal
    Posts: 10409
    125
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your utility class Test1 does not have a declared package, which puts it in the default package. You won't be able to import it unless you put it in a specific package that you can import.

    Edit: The clue that the compiler gives you is when it reports "." expected in your import Test1; statement.  The compiler thinks that Test1 is a package name, not a class name.
     
    Dave Tolls
    Ranch Hand
    Posts: 2378
    25
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's what I was beginning to suspect was the case.
    I've not written anything that wasn't in a package for rather a long time, so wasn't sure on the exact limitations.
     
    Filipe Madureira
    Ranch Hand
    Posts: 42
    1
    Chrome Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes! Now it's working properly! Thank you so much <3. To be honest i'm quite confused about what packages are and why i needed one, i'll look into it now.

     
    Junilu Lacar
    Marshal
    Posts: 10409
    125
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Filipe Madureira wrote:Yes! Now it's working properly! Thank you so much <3. To be honest i'm quite confused about what packages are and why i needed one, i'll look into it now.

    Packages are just a higher level of organization. A package normally contains a number of related classes.  Look over the standard Java Library API Documentation to see how classes are organized into different packages to get a sense of how you'd want to do it for your own programs.
     
    Campbell Ritchie
    Marshal
    Posts: 53171
    124
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Read about packages in the Java™ Tutorials. An IDE will usually sort out the package structure for you automatically.
     
    my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
    the new thread boost feature brings a LOT of attention to your favorite threads
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!