2.3. Calling Methods Without Parameters — AP CSAwesome (2023)

Methods are a set of instructions that define behaviors for all objects of a class. For example, in the Turtle class, methods like forward() and turnRight() give Turtle objects the ability to move forward and turn 90 degrees right.

To use an object’s method, you must use the object name and the dot (.) operator followed by the method name, for example, yertle.forward(); calls yertle’s forward method to move a turtle object forward 100 pixels. These are called object methods or non-static methods. An object method must be called on an object of the class that the method is defined in. Object methods work with the attributes of the object, such as the direction the turtle is heading or its position.

Every method call is followed by parentheses. The parentheses () after method names are there in case you need to give the method parameters (data) to do its job, which we will see in the next lesson. You must always include the parentheses after the method name.


object.method(); is used to call an object’s method.

Check Your Understanding: Mixed-up Code

The following code uses a turtle to draw the digital number 7, but the lines are mixed up. Drag the code blocks to the right and put them in the correct order to first draw the line going up (towards the top of the page) and then turn and draw a line to the left to make a 7. Remember that the turtle is facing the top of the page when it is first created. Click on the “Check Me” button to check your solution.

 public class DrawL{--- public static void main(String[] args) {--- World habitat = new World(300,300);--- Turtle yertle = new Turtle(habitat);--- yertle.forward();--- yertle.turnLeft(); yertle.forward();--- habitat.show(true);--- } // end main---} // end class 

Coding Exercise:

After you put the mixed up code in order above, type in the same code below to make the turtle draw a 7.(If the code below does not work for you, you can also use the Turtle code at this repl.it link (refresh page after forking and if it gets stuck) or download the files here to use in your own IDE.)

Coding Exercise:

Can you make yertle draw the digital number 8, as 2 squares on top of each other?

2.3.1. Procedural Abstraction

Procedural abstraction allows a programmer to use a method and not worry about the details of how it exactly works. For example, we know that if we hit the brakes, the car will stop, and we can still use the brakes even if we don’t really know how they work.

(Video) 2.3 Calling Methods Without Parameters | CSAwesome APCSA Java

You will learn to write your own methods in Unit 5. In this unit, you should be able to use methods already written for you and figure out what they do. When we use methods for a class in a library, we can look up the method signature (or method header), which is the method name followed by a parameter list, in its documentation. For example, here is a Student class with a method signature public void print() which has an empty parameter list with no parameters. Methods are defined after the instance variables (attributes) and constructors in a class.

Check Your Understanding

2-3-4: Click on the method headers (signatures) in the following class. Do not click on the constructors.Methods follow the constructors. The method header is the first line of a method.

public class Student { private String name; private String email; public Student(String initName, String intEmail)  { name = initName; email = initEmail; } public String getName()  { return name; } public void print()  { System.out.println(name + ":" + email); }}

The Java visualization below shows how a song can be divided up into methods. Click on the next button below the code to step through the code. Execution in Java always begins in the main method in the current class. Then, the flow of control skips from method to method as they are called. The Song’s print method calls the chorus() and animal() methods to help it print out the whole song.

When you call the chorus() method, it skips to the chorus code, executes and prints out the chorus, and then returns back to the method that called it.

Activity: CodeLens (songviz1)

Methods inside the same class can call each other using just methodName(), but to call non-static methods in another class or from a main method, you must first create an object of that class and then call its methods using object.methodName().

Check your understanding

    2-3-6: What does the following code print out?

     public class Song { public void print() { System.out.print("I like to "); eat(); eat(); eat(); fruit(); } public void fruit() { System.out.println("apples and bananas!"); } public void eat() { System.out.print("eat "); } public static void main(String[] args) { Song s = new Song(); s.print(); }}
  • I like to eat eat eat.
  • Try tracing through the print method and see what happens when it calls the other methods.
  • I like to eat eat eat fruit.
  • There is a fruit() method but it does not print out the word fruit.
  • I like to apples and bananas eat.
  • The order things are printed out depends on the order in which they are called from the print method.
  • I like to eat eat eat apples and bananas!
  • Yes, the print method calls the eat method 3 times and then the fruit method to print this.
  • Nothing, it does not compile.
  • Try the code in an active code window to see that it does work.

Try this visualization to see this code in action.


method(); is used to call a method within the same class, but object.method(); is necessary if you are calling the method from the main method or from a different class.

(Video) AP CSA CSAwesome 2.3, 2.4, 2.5 Methods with and without parameters and return values

Before you call a method from main or from outside of the current class, you must make sure that you have created and initialized an object. Remember that if you just declare an object reference without setting it to refer to a new object the value will be null meaning that it doesn’t reference an object. If you call a method on a variable whose value is null, you will get a NullPointerException error, where a pointer is another name for a reference.

2.3.2. Programming Challenge : Draw a Letter

Working in pairs, use the area below (or the repl.it link) to use a turtle to draw a simple block-style letter or number that uses just straight lines (no curves or diagonals). It could be one of your initials or a number from today’s date.

It may help to act out the code pretending you are the turtle. Remember that which way you turn depends on which direction you are facing, and the turtle begins facing north (towards the top of the page).

Here are some simple turtle methods that you can use:

You may notice that it is challenging to have your turtle draw with these simple methods. In the next lesson, we will use more complex Turtle methods where you can indicate how many steps to take or what angle to turn that will make drawing a lot easier!

Create a drawing of a simple letter or number that uses just straight lines (no curves or diagonals). It could be an initial in your name or a number from today’s date.

2.3.3. Summary

  • Methods are a set of instructions that define the behaviors for all objects of the class.

  • Use dot notation to execute an object’s method. This is the object’s name followed by the dot (.) operator followed by the method name and parentheses: object.method();

  • A method signature is the method name followed by the parameter list which gives the type and name for each parameter. Note that methods do not have to take any parameters, but you still need the parentheses after the method name.

  • Procedural abstraction allows a programmer to use a method by knowing in general what it does without knowing what lines of code execute. This is how we can drive a car without knowing how the brakes work.

  • A method or constructor call interrupts the sequential execution of statements, causing the program to first execute the statements in the method or constructor before continuing. Once the last statement in the method or constructor has executed or a return statement is executed, the flow of control is returned to the point immediately following the method or constructor call.

  • A NullPointerException will happen if you try to call an object method on an object variable whose value is null. This usually means that you forgot to create the object using the new operator followed by the class name and parentheses.

    (Video) CSAwesome: Lesson 2.3

  • An object method or non-static method is one that must be called on an object of a class. It usually works with the object’s attributes.

  • A static method or class method method is one that doesn’t need to be called on an object of a class.

2.3.4. AP Practice

    2-3-8: Consider the following class definition.

    public class Party{ private int numInvited; private boolean partyCancelled; public Party() { numInvited = 1; partyCancelled = false; } public void inviteFriend() { numInvited++; } public void cancelParty() { partyCancelled = true; }}

    Assume that a Party object called myParty has been properly declared and initialized in a class other than Party. Which of the following statements are valid?

  • myParty.cancelParty();

  • Correct!

  • myParty.inviteFriend(2);

  • The method inviteFriend() does not have any parameters.

  • myParty.endParty();

  • There is no endParty() method in the class Party.

  • myParty.numInvited();

  • There is no numInvited() method in the class Party. It is an instance variable.

  • System.out.println( myParty.cancelParty() );

  • This would cause an error because the void method cancelParty() does not return a String that could be printed.

    2-3-9: Consider the following class definition.

    public class Cat{ public void meow() { System.out.print("Meow "); } public void purr() { System.out.print("purr"); } public void welcomeHome() { purr(); meow(); } /* Constructors not shown */}
    (Video) 2.5 Calling Methods That Return Values | CSAwesome APCSA Java

    Which of the following code segments, if located in a method in a class other than Cat, will cause the message “Meow purr” to be printed?

  • Cat a = new Cat();Cat.meow();Cat.purr();
  • You must use the object a, not the class name Cat, to call these methods.

  • Cat a = new Cat();a.welcomeHome();
  • This would print “purrMeow “

  • Cat a = new Cat();a.meow();a.purr();
  • Correct!

  • Cat a = new Cat().welcomeHome();
  • This would cause a syntax error.

  • Cat a = new Cat();a.meow();
  • This would just print “Meow “.

You have attempted of activities on this page


Which of the following methods accepts no parameters? ›

The length() method does not require any parameters.

What does no parameters mean? ›

parameter-list is the list of parameters that the function takes separated by commas. If no parameters are given, then the function does not take any and should be defined with an empty set of parenthesis or with the keyword void. If no variable type is in front of a variable in the paramater list, then int is assumed.

How do you write a method without parameters in Java? ›

Java Program to Create a Method without Parameters and Return...
  1. public class Test.
  2. {
  3. void areacircle()
  4. {
  5. System. out. print("enter the radius :");
  6. Scanner s = new Scanner(System. in);
  7. float r = s. nextFloat();
  8. float ar;

What happens when a function is called without parameters? ›

javascript will set any missing parameters to the value undefined . This works for any number of parameters.

Can you call a function without all parameters? ›

Defining a Function Without Parameters

We can call the function by typing its name followed by parentheses () .

Can a method have zero parameters? ›

The lesson brief states that “Functions can have zero, one or more parameters”.

What are the four types of parameters? ›

Supported parameter types are string, integer, Boolean, and array.

Which of the following method can be used to get the value of form parameter Mcq? ›

getParameter() method to get the value of a form parameter. Q 18 - Which of the following method can be used to get the multiple values of a parameter like checkbox data?

What is parameters example? ›

A parameter is used to describe the entire population being studied. For example, we want to know the average length of a butterfly. This is a parameter because it is states something about the entire population of butterflies.

What is parameter short answer? ›

parameter, in mathematics, a variable for which the range of possible values identifies a collection of distinct cases in a problem.

How do you fix a parameter error? ›

The most effective way to fix the "The parameter is incorrect" error is to perform high-level formatting, also called quick format in Windows. It will reset the file system of the target device, making it accessible again. Step 2. Open File Explorer and right-click the inaccessible device and select "Format".

How do you call a method without main method? ›

Yes, we can execute a java program without a main method by using a static block. Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by Java ClassLoader, It is also known as a static initialization block.

Can we call method without call in Java? ›

This is not possible in java. You cannot call an method without an object.

How do you call a function without function sending a parameter? ›

Answer: here are two ways to pass parameters in C: Pass by Value, Pass by Reference. Pass by Value. Pass by Value, means that a copy of the data is made and stored by way of the name of the parameter. ...

What is parameters without type? ›

In the above function definition variable ' b' is the parameter and the value passed to the variable. 'b' is the argument.  The precondition (requires) and postcondition (returns) of the function is given.  We have not mentioned any types: (data types). This is called parameter without type.

How do you call a function without parameters in PL SQL? ›

Functions that take no parameters are written without parentheses. The function body begins with the keyword IS (or AS ) and ends with the keyword END followed by an optional function name. The function body has three parts: an optional declarative part, an executable part, and an optional exception-handling part.

Do functions always need parameters? ›

Parameters are essential to functions, because otherwise you can't give the function-machine an input.

What is a parameter in a function call? ›

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. For example: function example(parameter) { console.

How many parameters does a method need? ›

Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

Is there a limit of parameters for a method? ›

There is really no hard rule for the number of parameters to a method or constructor that is too many.

Can we call method with null object? ›

If you call a static method on an object with a null reference, you won't get an exception and the code will run. This is admittedly very misleading when reading someone else's code, and it is best practice to always use the class name when calling a static method.

What are 3 modes of parameters? ›

PL/SQL procedure parameters can have one of three possible modes: IN, OUT, or IN OUT.

What are basic parameters? ›

A parameter is a numerical attribute of the entire population. For example, the average or mean value of the population would be a parameter. Whereas, a statistic is a numerical attribute of the sample or the subsample. For example, the average value of some sample property is a statistic of that sample.

What are the 2 types of function parameters? ›

Mandatory and Optional Parameters

That is, when we initialise a parameter with a default value, it becomes optional. Otherwise, the parameter will be mandatory. In the above example, man1 and man2 are mandatory because they are not initialised in the function definition.

What is parameter in statistics Mcq? ›

A parameter has a sampling distribution that can be used to determine what values the statistic is likely to have in repeated samples. C. A parameter is used to estimate a statistic.

What are the two different types of values for a parameter Mcq? ›

What are the two different types of values for a parameter?
  • Default and System given.
  • Default and user given.
  • System given and user given.
  • Compile time and run time values.
1 Oct 2019

Which method has the same name as that of its class Mcq? ›

3. Which of the following is a method having same name as that of it's class? Explanation: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.

How do you write a parameter? ›

To specify parameters in JavaScript, we write the name of the parameter (or parameters) inside the parentheses that come after the function name. We then reference that parameter name inside the function. Now we can call that function and pass our desired value for the parameter: sayHello("Jackson"); sayHello("Mr.

Why is it called a parameter? ›

In these networks there are four parameters called hybrid or h-parameters. Out of these four parameters, one is measured in ohm, one in mho and other two are dimension less. Since these parameters have mixed dimension, so they are called hybrid parameters.

How do you calculate parameters? ›

To calculate the learnable parameters here, all we have to do is just multiply the by the shape of width m, height n, previous layer's filters d and account for all such filters k in the current layer. Don't forget the bias term for each of the filter.

What is the parameter symbol? ›

Parameters are usually Greek letters (e.g. σ) or capital letters (e.g. P). Statistics are usually Roman letters (e.g. s). In most cases, if you see a lowercase letter (e.g. p), it's a statistic. This table shows the different symbols.

Why do we use parameter? ›

A parameter is a special kind of variable used in a function to refer to one of the pieces of data provided as input to the function. These pieces of data are the values of the arguments with which the function is going to be called/invoked.

What is a parameter in data? ›

Parameters are numbers that summarize data for an entire population. Statistics are numbers that summarize data from a sample, i.e. some subset of the entire population.

What means parameter error? ›

The Parameter Problem error message generally means that something is wrong with the IP datagram itself, and that the datagram is being discarded. Parameter Problem errors almost always result from an incorrect usage of an IP option.

How do you reset parameters? ›

On any Ground Control Station (that can write parameters) you can reset parameters by setting the FORMAT_VERSION parameter to zero. After writing the parameter, reboot the board.

What is parameter validation error? ›

The error occurs when the value of the child stack that's passed from the parent stack doesn't match the parameter type. The error also occurs when the parameter's resource doesn't exist in the account in that Region.

What are the 3 types of Java variables? ›

There are three types of variables in Java: Local, Instance, and Static.

What are the 4 types of variables in Java? ›

In Java, there are different types of variables, for example:
  • String - stores text, such as "Hello". ...
  • int - stores integers (whole numbers), without decimals, such as 123 or -123.
  • float - stores floating point numbers, with decimals, such as 19.99 or -19.99.
  • char - stores single characters, such as 'a' or 'B'.

What are the 3 types of Java? ›

Java Platform, Standard Edition (Java SE) Java Platform, Enterprise Edition (Java EE) Java Platform, Micro Edition (Java ME)

Can we have 2 main methods in Java? ›

Yes. While starting the application we mention the class name to be run. The JVM will look for the main method only in the class whose name you have mentioned.

Can we execute code without main? ›

Yes, you can compile and execute without main method by using a static block. However, after static block executes, you will get an error saying no main method found.

What is main () method? ›

The main() is the starting point for JVM to start execution of a Java program. Without the main() method, JVM will not execute the program. The syntax of the main() method is: public: It is an access specifier.

What is the __ call __ method? ›

The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When the instance is called as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x. __call__(arg1, arg2, ...) .

How many types we can call method in Java? ›

There are two types of methods in Java: Predefined Method. User-defined Method.

Can we call method on primitive? ›

Because the argument you pass - an int is a primitive, and primitives are not objects - you can't invoke methods on them.

Can a method be called without an object? ›

You cannot call an method without an object.

Can a function work without arguments? ›

Functions do not have declared return types. A function without an explicit return statement returns None . In the case of no arguments and no return value, the definition is very simple. Calling the function is performed by using the call operator () after the name of the function.

Can we call main method with null? ›

Yes, main method have a signature as any other method. And array in java are nullable.

What is function without argument without return value? ›

Example 1: No Argument Passed and No Return Value

The return type of the function is void . Hence, no value is returned from the function.

What are the parameters of a function called? ›

The parameters, in a function call, are the function's arguments.

How do you access a method without using an object? ›

We can call a static method by using the ClassName. methodName. The best example of the static method is the main() method. It is called without creating the object.

Can a class exist without methods? ›

There is no class which doesn't have any method. All Classes are subclass of Object class. So all the methods from Object class are inherited.

What method can be called without a class? ›

Static methods are the methods in Java that can be called without creating an object of class.

How many arguments does a function need? ›

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

Can a function have 3 arguments? ›

We pass arguments in a function, we can pass no arguments at all, single arguments or multiple arguments to a function and can call the function multiple times.

How do you call a function with no arguments in C? ›

To call a function which takes no arguments, use an empty pair of parentheses. Example: total = add( 5, 3 );


1. CSAwesome: Lesson 2.3
(Mr. Rhodes)
2. 2.3/2.4: Void Methods & Parameters! AP Computer Science A
(The Eason)
3. 2.3 Calling a Void Method 2.4 Calling a Void Method with Parameters 2.5 Calling a Non-void Method
(Erik Wiessmann)
4. CSAwesome: Lesson 2.4
(Mr. Rhodes)
6. CSAwesome: Lesson 2.2
(Mr. Rhodes)
Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated: 02/03/2023

Views: 6042

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.