This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

User-defined types as fields

 
Ranch Hand
Posts: 115
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems as though with composition we create instance variables that refer to objects. I have a couple of questions with regard to this:

example: Main class plus 4 classes

1.  I would think that theCase is an instance variable of type Case, but my reading tells me that it is a custom data type variable of type instance. Is this correct?
2.  By declaring the instance variables in the class PC, are these instance variables referencing the objects theCase, theMonitor, and theMotherBoard that I created in the main method from the Main class?  



public Class Main {

public static void main(String[]Args) {

Case theCase = new Case();
Monitor theMonitor = new Monitor();
MotherBoard theMotherBoard = new MotherBoard();

}

}













 
Rancher
Posts: 498
7
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When a variable of custom type is declared using the new operator, it is now termed as instance.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your first case:

It is making the object of the Case class.

In your second case:


It is not instance variable if you making  instance variable you can declare datatype like int, string, double etc.
 
Marshal
Posts: 70725
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Randy Tong wrote:

Quite wrong. There is no object in that code, only an uninitialised reference. That is called a declaration.

When a variable of custom type is declared using the new operator, it is now termed as instance.

Wrng again. The new operator doesn't declare anything. It causes an instance to be created and you are now assigning it to the reference theCase.

Sherin Mathew wrote:In your first case:

It is making the object of the Case class.

That isn't RT's first case, but his second case. Only the right half of that line makes an object; the left half assigns it to a reference.



In your second case:


It is not instance variable if you making  instance variable you can declare datatype like int, string, double etc.

That doesn't make sense, and also looks wrong. Apart from that being RT's first case, it is in fact declaring an instance variable. It would be better to call it a field.
 
Sheriff
Posts: 15959
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:1.  I would think that theCase is an instance variable of type Case


correct

but my reading tells me that it is a custom data type variable of type instance. Is this correct?


incorrect

2.  By declaring the instance variables in the class PC, are these instance variables referencing the objects theCase, theMonitor, and theMotherBoard that I created in the main method from the Main class?


No, the objects created in the main() method are local to main()

You have many terms all jumbled up.

Instance - same thing as an object. The term "instance" is usually used in the context of a Class. That is, if Point is a class, then the object created by new Point() is an instance of it.

 
bryant rob
Ranch Hand
Posts: 115
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

bryant rob wrote:1.  I would think that theCase is an instance variable of type Case


correct

So, since Case is a variable type, is Case in this situation a custom data type?



 
Junilu Lacar
Sheriff
Posts: 15959
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A custom data type is just something that you have defined, as opposed to any "built-in" or "standard" types which are classes defined in the Standard Library, such as Integer, List, System, Stream, etc.

Since the Case class is something that you defined then, yes, it is a custom type.
 
Junilu Lacar
Sheriff
Posts: 15959
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:So, since Case is a variable type, is Case in this situation a custom data type?


Your phrasing is again jumbled here.

Case is a class. In that line, it's the declared type of the variable theCase.

If you really want to use the term "variable type" then it would be more correct to say "the variable type, i.e., the type of the variable theCase, is Case."
 
Campbell Ritchie
Marshal
Posts: 70725
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:. . . a custom data type . . .

You could call it that, but that is a very flattering name for something which might be very simple.
 
bryant rob
Ranch Hand
Posts: 115
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. I understand where my terminology was wrong and that I am reading more in to this than need be. I will just state that I created an object named theCase of type Case which is a user defined variable,  and leave it at that....I hope I have it right now.
 
Campbell Ritchie
Marshal
Posts: 70725
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is a user‑defined type, but every class you write is implicitly a user‑defined type.
 
Saloon Keeper
Posts: 12442
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:I will just state that I created an object named theCase of type Case which is a user defined variable,  and leave it at that....I hope I have it right now.


No. objects aren't named, unless you give them a field called "name".

What you did was create an instance (or object) of actual type Case and you assigned it to a local variable with formal type Case. The name of the variable is "theCase", not the name of the object.

"Defining" a variable means giving it a value, but it's much more common to just say "initialize" or "assign a value to". The word "defining" is much more commonly used to refer to to writing the body of a class, or the body of a method.

You meant "User declared variable", but that also doesn't mean much. Who is the user? The author of the class? Well, isn't the author of the class always the one who declares the variables in the class?
 
Junilu Lacar
Sheriff
Posts: 15959
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:... I am reading more in to this than need be. I will just state that I created an object named theCase of type Case which is a user defined variable,  and leave it at that....I hope I have it right now.


As Stephan noted, you're still not quite there. The following would be more correct:

1. "I created an object and assigned it to the theCase variable"

2. "I assigned a new Case object to theCase."

3. "I initialized theCase to a new instance of Case."

In 2 and 3 above, the word "variable" after "theCase" is implied. As Stephan noted, "user-defined" is usually implicit and in most cases redundant. Hence, the term "user-defined variable" is not something you'll see often and begs the question "As opposed to what, if not 'user-defined'?" Simply saying "variable" is enough. If you need to be specific, you can differentiate between a "reference variable" and a "primitive variable" where the former is assigned a reference to an object while the latter is assigned a primitive value (int, boolean, long, double, float, etc.)

Instead of using "user-defined," it's probably more useful to use "standard" to emphasize the fact that you're using a class that's defined in the standard library. In all other cases, most people will assume that you're likely referring to a custom/user-defined class.
 
Campbell Ritchie
Marshal
Posts: 70725
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or even,
  • 4: I am using the name theCase for an object of type Case.
  •  
    bryant rob
    Ranch Hand
    Posts: 115
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the info guys. I totally am understanding this now.I really do appreciate it.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 12442
    269
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:In all other cases, most people will assume that you're likely referring to a custom/user-defined class.


    Altough here too, the word "user" is ambiguous. I would avoid it here and only use it when you're referring to the person who is interacting with the running application.
     
    The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
    the value of filler advertising in 2020
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic