1

This is a very basic question. I am new to Java and programming in general. I am familiarizing myself with a rather large code, and I do not understand the purpose or function of a particular type of line. I understand the example lines below and have included comments to describe what is happening.

public class MainClass;

    private static final int SOME_CONSTANT = 2;  // declares and initializes constant
    private String someMessage; // declares an object of type String

However, I do not understand what is happening below. These types of declarations occur directly after the above code in the same MainClass.

    private DifferentClass differentClass; // declaring an object of type DifferentClass??
    public AnotherDifferentClass anotherDifferentClass; // same thing?

Are objects being declared here in the same way that String objects are declared?

I know this is very basic stuff. I appreciate any assistance provided. If interested, feel free to direct me towards any additional resources/reading that you may have found helpful when you were learning.

Bill
  • 89
  • 1
  • 7
  • 1
    Only references are being created, no objects are being created anywhere. – Nicholas K Jan 14 '19 at 19:11
  • 1
    _"I have been tasked with documenting a rather large code"_ Be aware, your current comments aren't really documenting the code: it doesn't describe the intent. You may want to confirm with whoever assigned you this task if this is really what they meant. – Mark Rotteveel Jan 14 '19 at 19:33
  • Mark, thank you. The documentation was outside the scope of this question. I changed the question to remove any ambiguity about my comments which are only provided to illustrate my degree of understanding of whatever is happening at a particular line. – Bill Jan 14 '19 at 19:43
  • @NicholasK References aren't being created here, either, only a blank space to hold them. – chrylis -cautiouslyoptimistic- Jan 14 '19 at 20:24

5 Answers5

2

There are in general two types of datatypes in Java:

  • Reference types: e.g. String, DifferentClass, MainClass, arrays... (any class you can instantiate). When you declare an object of this type, your variable stores an address in your memory, where the data (object) is actually stored.
  • Primitive types: e.g. simple types like: int, char... When you declare a primitive type, your variable stores the value of the primitive type.

To answer your question:
Now you see that all fields (String, DifferentClass, AnotherDifferentClass) are reference types, thus the same happens when you declare any of them.

FilipR
  • 1,218
  • 4
  • 22
  • 39
2

Yes and no.

Your first example private DifferentClass differentClass; declares a new object of type DifferentClass with name differentClass. The private keyword signifies that the particular object can only be used within MainClass.

Your second example public AnotherDifferentClass anotherDifferentClass; also declares a new object of type AnotherDifferentClass with the name anotherDifferentClass. However, the use of public is important. This means this object can now be accessed and updated outside of MainClass. If someone makes a new MainClass object, they could then do something like mainClassObj.anotherDifferentClass = [a AnotherDifferentClass object]. For more on the difference between public vs private, check out this StackOverflow Question.

Shayon Saleh
  • 419
  • 3
  • 7
  • 15
1

In the example that you understand, you have the line

private String someMessage; // declares an reference to an object of type String

String is a different class than MainClass, so it is exactly the same behavior, you could consider it as

private DifferentClass someMessage; // declares an reference to an object of type DifferentClass
Ricola
  • 2,621
  • 12
  • 22
1

Yes! Object Oriented programming languages help you define your own "Types" with constructs like Classes and Structures.

String is a "type" of data that holds string of characters. You can ask various questions to the "type" such as what's the length of the string (the number of characters the string contains) and also do some nifty operations on them such as finding substrings etc.

Similarly DifferentClass and AnotherDifferentClass are also "types" of data your program can hold.

Data can be an abstract concept or something quite concrete.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

It is the same as what is happening with

private String someString;

A reference is being created for an object of class someClass. This object is instantiated later in the code.

Bill
  • 89
  • 1
  • 7