Search the courses, subjects, tracks, games and features, or jump straight to a page.
5 cards from this section. Quick, good the night before a test.
Where can a private field of a class be used?
Private is the narrowest of the four access levels.
Which of these can be declared private: a field, a method, a constructor, a top-level class?
Ask what 'usable only inside its own class' could mean for a class that is not inside anything.
Which code can call a private method of class Shop?
Private is the narrowest access level.
A file begins with the line private class Shop. What does the compiler report?
Think about what 'private' would have to be private to.
Write a class Student with private fields name and an int array marks for five subjects. The constructor should copy the marks, replacing any mark outside 0 to 100 with 0 and printing a warning. Add a private helper total(), and public methods average() and grade(), where the grade is A for an average of 80 or more, B for 60 or more, C for 40 or more and D otherwise. In main, create two students and print each one's average and grade.
Validate in the constructor, and let average() call the private total().
A school canteen gives out at most three meal tokens a day. Write a class Token whose constructor is private, with a private static counter. A public static method issue(String name) should create and return a new Token while fewer than three have been issued, and otherwise print a message and return null. Show in main that four requests produce only three tokens.
Only code inside Token can call a private constructor, so put the limit check in a static method of Token.
6 more questions in this section. Enroll free to practise them all.
Enroll free