File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Inner working of Object Creation, methods, arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner working of Object Creation, methods, arguments" Watch "Inner working of Object Creation, methods, arguments" New topic
Author

Inner working of Object Creation, methods, arguments

John Natto
Greenhorn

Joined: Apr 12, 2013
Posts: 6
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

Joined: Feb 26, 2001
Posts: 4419
    
    5

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
John Natto
Greenhorn

Joined: Apr 12, 2013
Posts: 6
I really appreciate the explanation. Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner working of Object Creation, methods, arguments
 
Similar Threads
Quick question about the date class
Collections.sort()
problem with -cp
Read file from directory, update contents of the each file
== returning false even when hashcodes are same