NewbieNick
NewbieNick

Reputation: 89

How does Main have access to class?

I'm new to coding and I made this program that allows someone to input the song name, artist, album, year released and I just print it out. So while I CAN write the program, I don't understand how main gains access to the the CD class. How did the computer know I was creating a CD from this other class I had? Why was I able to use the accessor methods from the CD class? Sorry if this sounds dumb but I'm trying to understand why it works! THANK YOU FOR ANSWERS.

Thank you to all who answered! It makes sense now!

package project1;

public class CD {
    private String song;
    private String artist;
    private String album;
    private int year;

    public CD(String givenSong, String givenArtist, String givenAlbum, int givenYear)
    {
        song=givenSong;
        artist=givenArtist;
        album=givenAlbum;
        year=givenYear;
    }
    public String getSong(){
        return song;
    }
    public int getYear(){
        return year;
    }
    public String getArtist(){
        return artist;
    }
    public String getAlbum(){
        return album;
    }
}

and main..

package project1;

public class CDTest {
    public static void main(String[] args){
        CD cd1= new CD("Sad machine","Porter Robinson","Worlds",2014);
        System.out.print(cd1.getSong()+", ");
        System.out.print(cd1.getArtist()+", ");
        System.out.print(cd1.getAlbum()+", ");
        System.out.print(cd1.getYear());
    }

}

Upvotes: 2

Views: 98

Answers (4)

fpsColton
fpsColton

Reputation: 873

You have access to the CD class because it's within the same package as your main method, so you have access without explicitly importing the other class.

The reason you can call methods on cd1 is because you have created an instance of the object (when you used the new keyword, you created an instance of the CD class, and you've named that instance cd1.).

The methods in CD are available from cd1 because you've set the access modifier to public on all of the methods. The public allows other objects to access the methods without restriction.

Also, the reason you cannot call these public methods directly from CD is because these methods are not marked as static. Non static methods cannot be called directly from the class, but only from an instance of the class. These methods are often referred to as "instance methods" instead of "static methods".

Upvotes: 1

rgettman
rgettman

Reputation: 178253

You defined both of your classes in the same package. Classes defined in the same package can refer to themselves with simple names, without the need for a fully-qualified name (e.g. project1.CD) or an import statement (e.g. import project1.*). Here's more information about Java packages.

The CDTest class can execute methods on the CD class because the methods are public -- accessible by any class. This is contrast to CDTest hypothetically accessing the members of the CD class. It can't, because they are private. Here's more information about access levels such as public and private

Upvotes: 2

Dave
Dave

Reputation: 888

Both classes are in the same package, and the methods on the CD class are also marked as "public", so they accessible. Look into the Java Trails if you have time, they start very basic and ramp up. They have some really good examples and explanations

-- http://docs.oracle.com/javase/tutorial/getStarted/index.html

Upvotes: 1

William Price
William Price

Reputation: 4102

Because you've declared them in the same package and they are compiled together and both exist on the classpath. If they were in different packages, you would need to have an import statement.

Upvotes: 2

Related Questions