PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] How to get access to variables from other class

 July 07, 2022     class, import, java, variables     No comments   

Issue

So I would like to use the variable "kierunek" from this class

public class Snake extends JPanel implements ActionListener{
char kierunek = 'D';

to this class

import pl.java.Snake.*;

public class MyKeyListener extends KeyAdapter {

importing shows "Unused import statement", I tried to find soulution, but there is nothing helping me


Solution

With Java you will have to either have to create a public Getter Method, or make the variable itself public.

As a object oriented Language Java differentiates between different access modifiers. More on that here: What is the difference between public, protected, package-private and private in Java?

Usually one should use Getter and Setter as accessors for the variables in order to avoid creating Problems.

More on why here: Why use getters and setters/accessors?



Answered By - Daniellllll
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing