Mock

🎯 Try Our Mock CBT Practice!

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

Go to Mock CBT

Sketchware Custom Variables – Public, Private, Static & Final Guide

Master Sketchware custom variables — public, private, protected, static, and final. Understand how each modifier works with examples, syntax
Sketchware Custom Variables – Public, Private, Static & Final Guide

Sketchware Custom Variables – Complete Guide

In Sketchware Pro (and Android Java), variables are used to store data that your app can use later. You can make them public, private, protected, static, or final — each one behaves differently. This guide explains each modifier with examples and best practices.

Variable Types public | private | static

1️⃣ Public Variables

Public variables can be accessed from anywhere in your app — from other activities or classes. They are useful for global data, configuration values, or shared objects.

public String BOT_TOKEN = "";

public int uploadCount = 0;

public boolean isLoggedIn = false;

✅ Use public when you need global access.
🚫 Don’t use it for private or sensitive data.

2️⃣ Private Variables

Private variables can only be used inside the same class. This keeps your code secure and prevents unwanted access.

private String password = "1234";

private int clickCount = 0;

💡 Best for internal data, counters, and UI control variables.

3️⃣ Protected Variables

Protected variables can be accessed by the class itself and its subclasses. It’s used in inheritance scenarios.

protected String userType = "Admin";

4️⃣ Static Variables

Static means the variable belongs to the class, not the object. It keeps the same value for all instances of the class.

public static String APP_VERSION = "1.0.0";

public static int totalUsers = 0;

💡 Use static for constants or counters that must stay the same across the app.

5️⃣ Final Variables

A final variable can only be assigned once. Once you set its value, it cannot be changed — perfect for constants.

public static final String APP_NAME = "My Sketchware App";

📊 Comparison Table

ModifierAccessible FromCan Change?Use Case
publicEverywhere✅ YesGlobal settings, shared data
privateSame class only✅ YesLocal data, security
protectedSame class & subclasses✅ YesInheritance and extensions
staticClass level (shared)✅ YesGlobal constants, counters
finalAny scope❌ NoUnchangeable constants

🧩 Example: Using All Variable Types Together

public class MainActivity extends Activity {

  public String username = "Guest";         // Accessible everywhere

  private String password = "1234";         // Only inside MainActivity

  protected String role = "User";           // Accessible by subclasses

  public static int totalUsers = 0;         // Shared counter

  public static final String APP_NAME = "Sketchware Guide"; // Constant value

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    totalUsers++;

    Toast.makeText(this, "Welcome " + username, Toast.LENGTH_SHORT).show();

  }

}

📚 Quick Tips

  • Use private for most variables inside a class.
  • Use public static for global constants.
  • Use final to prevent accidental modification.
  • Keep names descriptive and clear (e.g. uploadCount, not uc).

🏁 Conclusion

Understanding variable types helps you write cleaner, safer, and more efficient Sketchware code. Whether you need global constants or secure local data, choosing the right modifier makes all the difference.

Written by osunhive – Sketchware Pro Developer Blog

Post a Comment