Mock

🎯 Try Our Mock CBT Practice!

Prepare for Civil Defence, Fire Service, Correctional & Immigration exams online.

Go to Mock CBT

All Types of Custom Variables in Sketchware Pro (Public, Private, Protected, Static)

All Types of Custom Variables in Sketchware Pro (Public, Private, Protected, Static)
All Types of Custom Variables in Sketchware Pro (Public, Private, Protected, Static)

All Types of Custom Variables in Sketchware Pro

In Sketchware Pro (and Java for Android), a custom variable is a piece of memory that stores data. You can use variables to store numbers, text, lists, or even Android objects. The visibility and accessibility of a variable depend on its modifier — for example, public or private.

🔹 Access Modifiers Overview

ModifierWho Can AccessCommon Use
publicAny other classShared/global data
privateOnly inside this classInternal variables (recommended default)
protectedThis class + subclassesUsed when extending classes
(none)Classes in the same packageRarely used in Android
staticShared across all instancesGlobal constants or counters

🔹 Basic Data Type Variables

private int count = 0;              // Whole numbers

private float price = 0.0f;         // Decimal numbers

private double total = 0.0;         // High precision decimals

private boolean isActive = false;   // True or false

private char grade = 'A';           // Single character

🔹 String and Object Variables

private String username = "Guest";        // Text value

private File rootFolder = Environment.getExternalStorageDirectory(); // File path

private ArrayList<String> userList = new ArrayList<>(); // List of items

private HashMap<String, Object> userData = new HashMap<>(); // Key-value pairs

🔹 Android-Specific Types

private MediaPlayer player = new MediaPlayer();  // For playing audio

private Intent intent = new Intent();            // Used to start new activities

private Handler handler = new Handler();         // For running tasks later

private WebView webview1;                        // For displaying web pages

private Toast toast;                             // For showing messages

🔹 Public, Protected, and Static Examples

public String BOT_TOKEN = "";      // Accessible from anywhere

protected MediaPlayer bgPlayer;    // Accessible in subclasses

public static int totalUsers = 0;  // Shared across all instances

public static final String APP_VERSION = "1.0.0"; // Constant value

🔹 Combined Example: Mixed Variable Types

public class MainActivity extends Activity {

    // Private variables (safe, local)

    private int uploadCount = 0;

    private boolean isUploading = false;

    private String caption = "Auto Upload";

    private ArrayList<String> imagePaths = new ArrayList<>();

    // Public variables (accessible anywhere)

    public String CHAT_ID = "";

    public File rootFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    // Protected variable (accessible in subclasses)

    protected MediaPlayer player = new MediaPlayer();

    // Static variables (global/shared)

    public static int totalUploads = 0;

    public static String APP_VERSION = "1.0.0";

    // Android objects

    private Handler handler = new Handler();

    private JSONObject configData = null;

}

🔹 Required Imports (if you use these types)

import java.io.File;

import java.util.ArrayList;

import java.util.HashMap;

import org.json.JSONObject;

import android.os.Environment;

import android.os.Handler;

import android.media.MediaPlayer;
Tip: In Sketchware, when you drag components (like Button, WebView, or MediaPlayer) into your project, Sketchware automatically generates their corresponding custom variables for you. You only need to manually add extra custom variables for your own logic or data handling.

✅ Quick Reference Table

ModifierTypeExampleUse Case
privateintuploadCount = 0;Local number tracking
privatebooleanisUploading = false;True/false state
publicStringBOT_TOKEN = "";Telegram bot token
protectedMediaPlayerplayer = new MediaPlayer();Subclass audio player
public staticinttotalUploads = 0;Global counter
privateArrayList<String>imagePaths = new ArrayList<>();Dynamic list

Post a Comment