Gurjinder Singh
Gurjinder Singh

Reputation: 1

Feedback needed on Error regarding Private access from another class

Before I get called out for asking a question that is already asked. Hear me out please. When I looked for an answer, none of them had the same problem as me. so here I go. Whenever I compile my code, I get an error saying variable has private access in method from another class. I cant seem to figure out where this error is coming from. I have rebuilt my code but have had no success. So, I have 3 different classes for my program. the issue is with the class called Glue not having access to class All skins.

glue class

import java.util.Scanner;
import java.util.Random;
public class glue {
public static void main(String [] args) {
    Dank_Memes d = new Dank_Memes();
    allskins a = new allskins();
    Scanner s = new Scanner(System.in);
    int money;
    String makemoney = "";
    int makemoneycounter = 0;
    String caseselect = "";
    int selection = 0;
    String multistring = "";
    int multiint;
    Random rand = new Random();
    do {
        System.out.println(d.menue());
        selection = s.nextInt();
        if (selection == 1) {
            int random = rand.nextInt(10);
            System.out.println(a.allskins[random]);    
        }
    }while (selection != 4);
}
}

all skins class

public class allskins {
    private int allskins;

    public String[] allskins() {
        String[] allskins = {"Karambit | Autotronic",
                             "Karambit | Black Laminate",
                             "Karambit | Blue Steel",
                             "Karambit | Boreal Forest",
                             "Karambit | Bright Water",
                             "Karambit | Case Hardened",
                             "Karambit | Crimson Web",
                             "Karambit | Damascus Steel",
                             "Karambit | Doppler",
                             "Karambit | Fade",
                             "Karambit | Forest DDPAT",
                             "Karambit | Freehand",
                             "Karambit | Gamma Doppler",
                             "Karambit | Lore",
                             "Karambit | Marble Fade",
                             "Karambit | Night",
                             "Karambit | Rust Coa",
                             "Karambit | Safari Mesh",
                             "Karambit | Scorche",
                             "Karambit | Slaughter",
                             "Karambit | Stained",
                             "Karambit | Tiger Tooth",
                             "Karambit | Urban Masked"};
      return allskins;                        
    } 
}

dankmemes class (interface)

public class Dank_Memes {
private int dankmemes[];

public String menue() {
    return "1) Select a case you would like to open\n" +
           "2) All knifes\n" +
           "3) Karambit, Shadow Daggers, M9 Bayonet and Bowie\n" +
           "4) Flip Knife, Flachion Knife, Bayonet and Huntsman\n" +
           "5) Gut Knife and Butterfly Knife\n";
}

}

The error code I recieve is "allskins has private access in allskins" Thanks to anyone that can provide feedback.

Upvotes: 0

Views: 55

Answers (2)

tsolakp
tsolakp

Reputation: 5948

You are a good example of producing messy and difficult to debug code by not following some basic Java best practices. You problem is that the call "a.allskins[random]" in the main method loop is not calling "allskins" method on "allskins" class but tries to access it's "allskins" private field. Change the call like this "a.allskins()[random]". In addition please name the class Allskins, the method get allskins and either don't use allskins field or make it static and preinitialize with array that you create in the method. Same applies to Dark_Memes.

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

To call your function do

a.allskins()[random]

because a.allskins with a.allskins[random] mean the private int allskins not the array which is returned by public String[] allskins() function.

a.allskins here is an object and .allskins mean to access member of object a and use some IDE to get help and avoid forcefully writing the code

Upvotes: 1

Related Questions