Which of the following is not an attribute of arrays when used in user-defined classes?


Java Class Attributes

In the previous chapter, we used the term "variable" for x in the example (as shown below). It is actually an attribute of the class. Or you could say that class attributes are variables within a class:

Example

Create a class called "Main" with two attributes: x and y:

public class Main {
  int x = 5;
  int y = 3;
}

Another term for class attributes is fields.


Accessing Attributes

You can access attributes by creating an object of the class, and by using the dot syntax (.):

The following example will create an object of the Main class, with the name myObj. We use the x attribute on the object to print its value:

Example

Create an object called "myObj" and print the value of x:

public class Main {
  int x = 5;

  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println(myObj.x);
  }
}

Try it Yourself »


Modify Attributes

You can also modify attribute values:

Example

Set the value of x to 40:

public class Main {
  int x;

  public static void main(String[] args) {
    Main myObj = new Main();
    myObj.x = 40;
    System.out.println(myObj.x);
  }
}

Try it Yourself »

Or override existing values:

Example

Change the value of x to 25:

public class Main {
  int x = 10;

  public static void main(String[] args) {
    Main myObj = new Main();
    myObj.x = 25; // x is now 25
    System.out.println(myObj.x);
  }
}

Try it Yourself »

If you don't want the ability to override existing values, declare the attribute as final:

Example

public class Main {
  final int x = 10;

  public static void main(String[] args) {
    Main myObj = new Main();
    myObj.x = 25; // will generate an error: cannot assign a value to a final variable
    System.out.println(myObj.x);
  }
}

Try it Yourself »

The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...).

The final keyword is called a "modifier". You will learn more about these in the Java Modifiers Chapter.



Multiple Objects

If you create multiple objects of one class, you can change the attribute values in one object, without affecting the attribute values in the other:

Example

Change the value of x to 25 in myObj2, and leave x in myObj1 unchanged:

public class Main {
  int x = 5;

  public static void main(String[] args) {
    Main myObj1 = new Main();  // Object 1
    Main myObj2 = new Main();  // Object 2
    myObj2.x = 25;
    System.out.println(myObj1.x);  // Outputs 5
    System.out.println(myObj2.x);  // Outputs 25
  }
}

Try it Yourself »


Multiple Attributes

You can specify as many attributes as you want:

Example

public class Main {
  String fname = "John";
  String lname = "Doe";
  int age = 24;

  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println("Name: " + myObj.fname + " " + myObj.lname);
    System.out.println("Age: " + myObj.age);
  }
}

Try it Yourself »

The next chapter will teach you how to create class methods and how to access them with objects.



Main Content

Specifying Class Attributes

All classes support the attributes listed in the following table. Attributes enable you to modify the behavior of class. Attribute values apply to the class defined within the classdef block.

classdef (Attribute1 = value1, Attribute2 = value2,...) ClassName
   ...
end

Class Attributes

Attribute Name

Class

Description

Abstract

logical

(default = false)

If specified as true, this class is an abstract class (cannot be instantiated).

See Abstract Classes and Class Members for more information.

AllowedSubclasses

meta.class object or cell array of meta.class objects

List classes that can subclass this class. Specify subclasses as meta.class objects in the form:

  • A single meta.class object

  • A cell array of meta.class objects. An empty cell array, {}, is the same as a Sealed class (no subclasses).

Specify meta.class objects using the ?ClassName syntax only.

See Specify Allowed Subclasses for more information.

ConstructOnLoad

logical

(default = false)

If true, MATLAB® calls the class constructor when loading an object from a MAT-file. Classes defined with this attribute must have a no-argument constructor.

See Initialize Objects When Loading for more information.

HandleCompatible

logical

(default = false) for value classes

If specified as true, this class can be used as a superclass for handle classes. All handle classes are HandleCompatible by definition. See Handle Compatible Classes for more information.

Hidden

logical

(default = false)

If true, this class does not appear in the output of the superclasses or help functions.

InferiorClasses

meta.class object or cell array of meta.class objects

Use this attribute to establish a precedence relationship among classes. Specify a cell array of meta.class objects using the ? operator.

The fundamental classes are always inferior to user-defined classes and do not show up in this list.

See Class Precedence.

Sealed

logical

(default = false)

If true, this class cannot be subclassed.

Framework attributes

Classes that use certain framework base classes have framework-specific attributes. See the documentation for the specific base class you are using for information on these attributes.

Specifying Attributes

Attributes are specified for class members in the classdef, properties, methods, and events definition blocks. The particular attribute setting applies to all members defined within that particular block. You can use multiple properties, methods, and events definition blocks to apply different attribute setting to different class members.

Superclass Attribute Values Are Not Inherited

Class attributes settings are not inherited, so superclass attribute values do not affect subclasses.

Attribute Syntax

Specify class attribute values in parentheses, separating each attribute name/attribute value pair with a comma. The attribute list always follows the classdef or class member keyword, as shown:

classdef (attribute-name = expression, ...) ClassName

   properties (attribute-name = expression, ...)
      ...
   end
   methods (attribute-name = expression, ...)
      ...
   end
   events (attribute-name = expression, ...)
      ...
   end
end

Class-Specific Attributes

Some MATLAB classes define additional attributes that you can use only with the class hierarchies that define these attributes. See the specific documentation for the classes you are using for information on any additional attributes supported by those classes.

  • Expressions in Attribute Specifications

  • Trial Software
  • Trial Software
  • Product Updates
  • Product Updates

Can you have an array in a class?

Arrays as Class Member Data If an array is used as member data of a class, the member functions can add in error-checking and boundary protection. Note that the array is allocated to size 10, but the list can have up to 10 items in it -- the "list" is not always "full"!

What is class variable array?

A class is a heterogeneous data type: it defines any number of instance variables, each declared with its own individual name to store a different type of value. An array is a homogeneous data type: it defines one name storing an arbitrary number of indexed values, all declared to store the same type of value.

What are the types of arrays in Java?

In Java, there are two types of arrays: Single-Dimensional Array. Multi-Dimensional Array.

Which of the following can contain in an array?

Example: An array in JavaScript can hold different elements that can store Numbers, Strings, and Boolean in a single array.