# Running Java code

## Calling methods

{% tabs %}
{% tab title="Syntax" %}

```
%object%.<method name>(%objects%)
```

{% endtab %}

{% tab title="example.sk" %}

```
event-block.breakNaturally()
(last spawned creeper).setPowered(true)
player.giveExpLevels({_levels})
```

{% endtab %}
{% endtabs %}

Methods may be used as effects, expressions, and conditions. If used as a condition, the condition will pass as long as the return value of the method is not `false`, `null`, or `0`.

### Calling non-public methods

If the method you're trying to invoke is not public, you must prefix the method name with the declaring class in brackets. Since an object may have a non-public method with the same name in multiple superclasses, you must explicitly specify where to find the method.

{% code title="example.sk" %}

```
{_arraylist}.[java.util.ArrayList]fastRemove(1)
# or, if you have the declaring class imported:
{_arraylist}.[ArrayList]fastRemove(1)
```

{% endcode %}

### Calling overloaded methods

Generally, skript-mirror can infer the correct overloaded method to call from the arguments passed at runtime. If you need to use a certain implementation of a method, you may append a comma separated list to the end of the method name surrounded in brackets.&#x20;

{% code title="example.sk" %}

```
System.out!.println[java.lang.Object]({_something})
# or, if you have the parameter classes imported:
System.out!.println[Object]({_something})

Math.max[int, int](0, {_value})
```

{% endcode %}

## Calling fields

{% code title="Syntax" %}

```
%object%.<descriptor>!
```

{% endcode %}

{% hint style="info" %}
References to fields must end in `!` due to limitations in Skript's parser.
{% endhint %}

### Calling non-public fields

If the field you're trying to use is not public, you must prefix the field name with the declaring class in brackets. Since an object may have a non-public field with the same name in multiple superclasses, you must explicitly specify where to find the field.

{% code title="example.sk" %}

```
{_hashmap}.[java.util.HashMap]modCount!
# or, if you have the declaring class imported:
{_hashmap}.[HashMap]modCount!
```

{% endcode %}

## Calling constructors

{% tabs %}
{% tab title="Syntax" %}

```
[a] new %javatype%(%objects%)
```

{% endtab %}

{% tab title="example.sk" %}

```
new Location(player's world, 0, 0, 0)
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://skript-mirror.gitbook.io/docs/basics/running-java-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
