• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner working of Object Creation, methods, arguments

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really appreciate the explanation. Thanks.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic