17

I always thought a client of a class is one that uses a reference to access instance fields/methods of that class i.e. another class (from its own methods). But when I think about some details there are some things that I couldn't figure out.

In Java I know of a few places where you can place action statements.

  • Inside a method (instance/static)
  • Inline initialization (when you declare an instance variable like in private int x = 5;)
  • Static block

Maybe there are more that I don't know or remember.

The first part of the question is are all of these considered clients if they are in another class? Also can they access private stuff when they are in the same class?

The second part is: When JVM calls a method (like main and finalize) are they considered clients, too? (the calls are not from a class?)

Edit: Can a client access only public methods and variables? Can't we call it a client if it accesses package fields (if in the same package).

Insignificant Person
  • 863
  • 2
  • 10
  • 24
  • 1
    You are asking for formal specifications of a colloquial term.That makes no sense. Anyone can define the term “client” as (s)he wishes. – Holger Jun 02 '15 at 10:42

5 Answers5

33

Before diving into Java -lets try to map the situation to a physical world scenario.

You go to a pastry shop and order a pastry. You get the pastry from the store pay for it and leave. You naturally call yourself a client of the store. So what did we understand from this?

The term client refers to any entity that requests a service from another entity. The client does not bother about how the entity providing the service actually provides the service - the client is happy as long as the service is available and fulfills its use case.

Hence, when a method M1 within a class calls another method M2 then M1 is a client of M2. Similarly when a class C1 requests service of C2 then C1 is a client of C2.

Coming to your question about main(), finalize() and the interaction with JVM - You can consider the class-loader of the JVM as the client of your class since it loads JVM class loader will load your class and then request the main method to begin execution and continue further processing. EDIT based on comment from the OP - finalize() method is accessed by the Garbage Collector within the JVM using some internal JVM tricks. As a general rule within any normal application will not perform such trickery.

Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
5

I don't believe there exists a strict definition of "client of class". I usually say "client code", that is, one piece of code that uses another piece of code through some sort of contract. From this point of view, even JVM, when calling your class' finalize method, can be considered client code for the code of your class, as it works with your class through a specific interface defined in Object class (its finalize method).

Anyway, the point here is not how you call it, but what you make of it. Is calling JVM the client of your class means something to you, or affects the way you write your code? I think not. So I prefer to talk about the notion of contract (interface) between two pieces of code.

Forketyfork
  • 7,416
  • 1
  • 26
  • 33
4

... a client of a class is one that uses a reference to access ... fields/methods of that class

Correct but not limited to instances

... are all of these considered clients if they are in another class?

Yes methods and variables are clients of some other class if they reference it.

Note that inline initialization is actually a short hand for initialization in the constructor method, and static blocks for the static initializer.

can [clients] access private stuff [from some other class]?

Only if the other class permits it (like returning a private object from a getter method). Same for protected stuff unless it's in the same package, the JVM itself is not subject to private/protected limitations.

When JVM calls a method (like main and finalize) are they considered clients, too? (the calls are not from a class?)

The JVM or one of it's components is considered the client in that case. The calls may be from a class in the JVM depending on the JVM implementation.

user1133275
  • 2,642
  • 27
  • 31
2

A client of a class, in the generally accepted use of the term, is any other class that uses reference access methods (less likely fields due to the principle of encapsulation in OOP). What you have enumerated are not considered clients in any way. But the term is part of a common programming language, it is not like there is a big theory behind it. It is just what majority of the programmers understand by it.

Positive Navid
  • 2,481
  • 2
  • 27
  • 41
iullianr
  • 1,274
  • 10
  • 11
0

I'm not sure, that there is a strict definition of 'client' in Java. Actually, I'd rather suggest this term only regarding interfaces, since they enforce encapsulation and avoid static calls. Since there are different accessibility modes, which are applied to methods and fields, then it's hard to predict and define which exact call is 'client' and which is not. Moreover, another question rises: 'Should we call child class a "client" one since it may use parent's methods?'

sviklim
  • 1,054
  • 1
  • 15
  • 30