-2

I have noticed a situation where there is a class (say: ClassA) with variable declarations and various methods. And in another class (say: Class B), there is a method(MethodofClassB()) with the return type of the method as ClassA.

so it is like:

 Class A
  {
      variable i,j;

      public int MethodA()
      {
       //some operation
      }
  }

 Class B
  {
      variable x,y;

      public static A MethodB()
      {
       //some operation
        return obj;
      }
  }

1) I understand that MethodB() return an object of ClassA. Waty would be the use(the intention) of returning the object of ClassA

2) What is the reason for defining MethodB() as Public static. what would happen if static was not used for MethodB()

3)What would the returned objct look like. I mean if my method returned an integer, it would return some numerical value say '123' . If a method returns an object of a class, what would be in the returrned value.

please help me understand this with a small example

Bhushan
  • 18,329
  • 31
  • 104
  • 137
user3083590
  • 205
  • 2
  • 3
  • 10
  • 5
    Looks like a static factory method to me http://stackoverflow.com/questions/929021/what-are-static-factory-methods-in-java - saying that, it's pretty commonplace to return objects from methods in different classes for all kinds of different reasons - that's what OOP is all about. – Ant P Dec 27 '13 at 17:59
  • http://docs.oracle.com/javase/tutorial/java/index.html – Bhesh Gurung Dec 27 '13 at 18:04

2 Answers2

4

1) I understand that MethodB() return an object of ClassA. Waty would be the use(the intention) of returning the object of ClassA

Depends on what the method does, which isn't illustrated in this example. If the result of the operation is an instance of A then it stands to reason that it would return an instance of A, whatever A is.

For example, if A is a Car and B is a CarFactory then the method is likely producing a new Car. So it would return a Car that's been produced.

2) What is the reason for defining MethodB() as Public static. what would happen if static was not used for MethodB()

public allows it to be accessed by other objects. static means it's not associated with a particular instance of B. Both are subjective based, again, on the purpose of the method (which isn't defined in the example). Being static, it can be called as such:

var newInstance = B.MethodB();

If it wasn't static then an instance of B would be required:

var objectB = new B();
var newInstance = objectB.MethodB();

There are more and more implications here, including things like memory/resource usage and thread safety. All stemming from the purpose and business logic meaning of what B is and what MethodB does.

3)What would the returned objct look like. I mean if my method returned an integer, it would return some numerical value say '123' . If a method returns an object of a class, what would be in the returrned value.

It would be an instance of A. Similar to creating an instance here:

var objectA = new A();

This method also creates (or in some way gets) an instance:

var objectA = B.MethodB();

Without knowing more about what A is, what its constructor does, and what MethodB does, these two operations are otherwise the same.

David
  • 208,112
  • 36
  • 198
  • 279
  • The question is tagged with `Java` (and not `JavaScript`). – PM 77-1 Dec 27 '13 at 18:13
  • @PM77-1: When I started writing the answer (and, indeed, when I completed writing the answer, according to the revision history) it was also tagged with C#, which is what I used in my answer. – David Dec 27 '13 at 18:14
  • Could you then clarify in your answer that you're using `C#` syntax? – PM 77-1 Dec 27 '13 at 18:16
-1

First, your code is incorrect. There is no "ClassA" class. The class name is A, so the return type should be A not ClassA.

Second, the standard Java coding standards say to start methods and variables with lower case letters. So, your example should have been:

 Class A
  {
      A anA;
      B aB;

      public int methodA()
      {
       //some operation
      }
  }

 Class B
  {
      SomeType x, y;

      public static A methodB()
      {
       //some operation
        return obj;
      }
  }

David's answer shortly before mine is technically correct on points 1 and 2, although he also uses your mistake of calling the A type ClassA. His code for his answer to point 3, though, is incorrect and misleading. I would change his wording to this:

`3)What would the returned objct look like. I mean if my method returned an 
integer, it would return some numerical value say '123' . If a method returns
an object of a class, what would be in the returrned value`.

It would be an instance of class A. Similar to creating an instance here:

A objectA = new A();

This method also creates (or in some way gets) an instance:

A objectA = B.methodB();

Without knowing more about what class A is, what its constructor does, and what methodB does, these two operations are otherwise the same.

dsa42
  • 55
  • 4
  • By the way, these are EXTREMELY basic questions, usually covered in the very first undergraduate programming class. – dsa42 Dec 27 '13 at 18:20
  • Dont be rude. Also in c# public methods starts with capital letter :) – Softwarehuset Dec 27 '13 at 20:03
  • I wasn't being rude. I was explaining that they are basic questions, to hopefully inspire the questioner to look into CS courses. Finally, the question was tagged with Java. I note the questioner has updated his question using my suggestions. I think the several people who voted down the question obviously agree with me. – dsa42 Dec 27 '13 at 20:37
  • 1
    @dsa42 SO isn't about downvoting basic questions. His question was clear and backed up with code examples. No need to shove it down the OP's throat that he/she is asking a simple question. Without the basic questions, SO would just be a bunch of elitist jerks posting questions about "advanced programming techniques" with nothing to offer new developers. I agree though the OP should consider some basic OOP classes or tutorials. – Evan L Dec 27 '13 at 21:02
  • @Evanlewis I didn't down vote the question. Others did. And they down voted the question before I answered it. All I did was explain that this was a basic question and explain how the first question was incorrect. – dsa42 Jan 03 '14 at 14:56