Key Concepts

Review core concepts you need to learn to master this subject

The public and private keywords

In Java, the keywords public and private define the access of classes, instance variables, constructors, and methods.

private restricts access to only the class that declared the structure, while public allows for access from any class.

Access, Encapsulation, and Scope
Lesson 1 of 2
  1. 1
    As our Java programs begin to get bigger and we begin to have multiple Objects and Classes that interact with each other, the concepts of access and scope come into play. To oversimplify things…
  2. 2
    After running the code in the last exercise, you should be developing an intuition on what the public and private keywords are doing. These keywords are defining what parts of your code have _acces…
  3. 3
    By now you’re probably catching onto what the private keyword does. When a Class’ instance variable or method is marked as private, that means that you can only access those structures from elsewhe…
  4. 4
    When writing classes, we often make all of our instance variables private. However, we still might want some other classes to have access to them, we just don’t want those classes to know the _exac…
  5. 5
    In addition to access modifiers like public and private, the scope of the variable also determines what parts of your code can access that variable. The scope of a variable is determined by wh…
  6. 6
    Often times when creating classes, programmers will create local variables with the same name as instance variables. For example, consider the code block below: public class Dog{ public String n…
  7. 7
    We’ve seen how the this works with variables, but we can also use the this with methods. Recall how we’ve been calling methods up to this point: public static void main(String[] args){ Dog myDo…
  8. 8
    Now that we’ve seen how methods can call other methods using this., let’s look at a situation where you might want to use private methods. Oftentimes, private methods are helper methods — tha…
  9. 9
    Nice work! In this lesson, we dove into some of the more subtle features of classes with a focus on access, encapsulation, and scope. Here are some of the main takeaways from this lesson: - The pu…

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo