Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Exactly is a Client of a Class

Tags:

java

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).

like image 333
Insignificant Person Avatar asked May 28 '15 17:05

Insignificant Person


People also ask

What is extend in Java?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

What is an instance method?

Instance method are methods which require an object of its class to be created before it can be called. To invoke a instance method, we have to create an Object of the class in which the method is defined.

What does client in class do in Java?

Client is the main entry point to the fluent API used to build and execute client requests in order to consume responses returned. Clients are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well as disposal of a Client instance may be a rather expensive operation.

What is a client in code?

A client is a computer or a program that, as part of its operation, relies on sending a request to another program or a computer hardware or software that accesses a service made available by a server (which may or may not be located on another computer).


1 Answers

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.

like image 155
Prahalad Deshpande Avatar answered Nov 16 '22 01:11

Prahalad Deshpande