Reputation: 79
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.
Upvotes: 1
Views: 786
Reputation: 2922
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
Upvotes: 1
Reputation: 79
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.
Upvotes: 0
Reputation: 429
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.
Upvotes: 2
Reputation: 1238
There are in general two types of datatypes in Java:
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.
Upvotes: 2
Reputation: 25705
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.
Upvotes: 1