Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner working of Object Creation, methods, arguments

 
John Natto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have just started learning Core Java, but have a couple of questions specially the initial elements of java because the way they get used and it is really confusing sometime. Could someone please explain me the concept behind the following statements?

1. A a1 = new A1
2. a1.test()
3. a1.test(90)
4. B b1 = null
5. Employee(Job [x])
6. return abc;


Thanks in advance.
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Natto wrote:Could someone please explain me the concept behind the following statements?

Not much to explain by way of concept; these snippets are declarations, assignments, method calls, and possibly instantiation of a new object.

1. A a1 = new A1

Declares variable a1 to be an instance of class A.
If A is an interface, declares a1 to be a reference to an object that implements A.
The right hand side is not valid Java. Valid Java would be new A1() where A1() is a no-argument constructor of the class A1.
A1 must be a subclass of A if A is a class.
A1 must be a class that implements A if A is an interface.
If the right hand side of the assignment operator (the = sign) were valid, it would assign that value to the variable a1
The value assigned would be a reference to an object.

2. a1.test()

Calls the method test() of the object referenced by a1. The method takes no parameters.

3. a1.test(90)

Calls the method test() of the object referenced by a1. This test() method declares one parameter which has a type that is compatible with the numeric constant 90 (e.g. int, long, Integer, etc.)

4. B b1 = null

Declares variable b1 to be an instance of B, if B is a class.
If B is an interface, declares b1 to be a reference to an object that implements B.
Assigns null to b1, meaning b1 does not refer to any object, even though it can.

5. Employee(Job [x])

Assuming standard Java coding conventions are followed, this is not valid Java. This looks like a constructor but you need the new keyword before the constructor call.
Also, the name "Job" does not follow not the standard naming convention for variables. However, it's not illegal syntax. At any rate, if Job is an array variable, then Job[x] refers to the x-th element of Job. Valid values of x start at 0 and end at (Job.length - 1)

6. return abc;

exits the current method and returns the value held by variable abc to the caller.

 
John Natto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate the explanation. Thanks.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Natto wrote:I really appreciate the explanation. Thanks.


Welcome to the Ranch!

For future reference, you'll generally get a more positive experience here if you do some groundwork before posting. SearchFirst(←click), ShowSomeEffort(←click), and TellTheDetails(←click) about what you read or tried, what you think about it, and where you're getting confused. If you do that, folks here will be happy to point you in the right direction.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic