• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

public class and package issues when trying to use access modifiers in IntelliJ IDEA

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there. This is pretty frustrating for me.



I've been using Pluralsight for my introduction into Java as a person who has no coding experience whatsoever.  I'm now in the "Applying Access Modifiers" lesson of the "Java Fundamentals: The Java Language" class offered by Jim Wilson. It has taught me how to use access modifiers quite well, however there is no detailing into how to use it with the IntelliJ program, as no matter what class, method and object I try to construct, either an error appears or the class itself is not identified (it keeps saying "class 'GuitarShop' is never used") in the IntelliJ IDEA Community program.

I use Java 1.8.

Following the "Applying Access Modifiers" lesson, I went ahead to create the project GuitarShop, It's a tool that counts the inventory of guitars and drums using classes, methods and access modifiers.

When I create a new project in IntelliJ, I use the "Groovy" framework, create my object from the Command Line App template, and make sure the the Project Name, Project Location and Base Package hold the same name as my public class. After creating a new project, for some reason that I'm surely ignorant of, the file is under the name of Main.java, which of course needs to be changed to match the public class name. When I rename Main.java (by right clicking the Main.java tab and clicking "Rename File", the rest of the (files?package names?) are seen as erroneous, with their tabs underlined in red. My public class GuitarShop, which I want to use for this file and others, displays "Class 'GuitarShop' is never used". This I think pretty much screws up the rest of my code, and renders this inoperable.

My field set the total amount of guitars to 100, and drums to 50. My method discounts the amount of guitars and drums sold (int gSold = guitars sold, dSold = drums sold) and prints out the remainder of them left.




GuitarShop.png
[Thumbnail for GuitarShop.png]
 
Greenhorn
Posts: 9
Mac OS X Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You're trying to create an object out of the class. Move it into the class, then it won't be red.

Hope this helps!
 
Ysid Brunski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Update. Thanks for the clarification! I did as you said and it helped. But when I run the code, this error message pops up.



GuitarShop2.png
[Thumbnail for GuitarShop2.png]
 
Ysid Brunski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ekrem Candemir wrote:Hi,

You're trying to create an object out of the class. Move it into the class, then it won't be red.

Hope this helps!



I thought that public classes are accessible to all objects even outside of the file.
 
Ekrem Candemir
Greenhorn
Posts: 9
Mac OS X Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget, main method is the entry point of any Java Program.

Create another class named whatever you want. I call it Main.java and it should be like this:

 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this course/book recommend using an IDE to do this?
Did it suggest using Groovy?

The issue you have just had re: Main.java is because you  are also learning the IDE as well as Java.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Good that you have got an answer, but in future please copy'n'paste the text, because your screenshots are very difficult to read.
 
Ysid Brunski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Good that you have got an answer, but in future please copy'n'paste the text, because your screenshots are very difficult to read.



Sure thing! The replies helped clear a few things, however I still get an error whenever I run my code.

 
Ysid Brunski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Does this course/book recommend using an IDE to do this?
Did it suggest using Groovy?

The issue you have just had re: Main.java is because you  are also learning the IDE as well as Java.



Yes to both. Sucks if learning the style of the IDE would be a necessity as well.

IntelliJ IDEA Community 2018.2.5
Groovy suggested
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think with no coding experience whatsoever that's a lot of moving parts to pick up on.
But that's where you;re at, I suppose.

What errors are you getting?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ysid Brunski wrote:I still get an error whenever I run my code.  


Well, there's two things I see.  One is that you've done something unusual: you've put an inner class inside the main().  But to understand what is happening, let's leave that for now.  

Inner classes can't be public, so change the access.

The next problem is trickier to spot.  Line up the braces ({}) very carefully.  You probably wanted this code:
in the main() method, but it's not.  See if you can figure out how to fix it.
reply
    Bookmark Topic Watch Topic
  • New Topic